zoukankan      html  css  js  c++  java
  • Python 文件编码(文件乱码)

    IndentationError: unindent does not match any outer indentation level

    文件未对齐,在记事本打开。

    乱码原因:
    源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了!

    解决方法:
    1、print mystr.decode('utf-8').encode('gbk')
    2、比较通用的方法:

    复制代码代码如下:

    import sys
    type = sys.getfilesystemencoding()
    print mystr.decode('utf-8').encode(type)

    发到哈工大

     

    打开文件:

    建立磁盘上的文件与程序中的对象相关联

    通过相关的文件对象获得

    读取

    写入

    定位

    其他:追加,计算等

    关闭文件

    切断文件与程序的联系

    写入磁盘,并释放文件缓冲区

    打开文件:

    open()

    <variable> = open (<name>,<mode>)

    <name>磁盘文件名

    <mode>打开模式

    egg:

    打开一个名为“numbers.dat”的文本文件

    >>>infile =  open ("numbers.dat","r")

    打开一个名为“music.mp3”的音频文件

    >>>infile = open("music.mp3","rb")

    def main()"
      fname = input ("Enter filename:")

      infile = open(fname,"r")

      data = infile.read()

      print(data)

    main()

    infile = open(someFile,"r")

    for i in range(5):

      line = infile.readline()

      print(line[:-1])

    >>>outfile = open("outfile.txt","w")

    >>>outfile .writelines(["Hello"," ","world"])

    >>>outfile.close()

    >>>infile = open("outfile.txt","r")

    >>>infile.read()

    'Hello world'

    python 读文件中的数字利用turtle画图

    http://www.icourse163.org/learn/bit-268001?tid=317001#/learn/content?type=detail&id=872018&sm=1

  • 相关阅读:
    Android自动化测试框架UIAutomator原理浅析
    UiAutomator和Appium之间的区别2
    UiAutomator、UiAutomator2、Bootstrap的关系
    好的博客和网站
    appium介绍和工作原理
    UiAutomator1.0 与 UiAutomator2.0
    Jenkins之配置GitHub-Webhook2
    jenkins部署github项目持续集成
    Windows下安装的Jenkins修改默认端口号8080(修改配置文件的方式)
    Git使用教程
  • 原文地址:https://www.cnblogs.com/XDJjy/p/4950521.html
Copyright © 2011-2022 走看看