Python:open的文件读取操作,utf-8,UnicodeDecodeError

简要目录:

open函数

将文件设置为utf-8编码格式

UnicodeDecodeError

f.read() 和 f.read(size)

f.readline() 和 f.readlines()

f.tell():返回文件指针的位置,注意换行符

f.writelines() 和 f.write()

f.seek():设置文件指针的位置 —— f.seek(偏移量,[起始位置])

文件指针

with open:可以不需要显式关闭文件操作:f.close()

f.__next__():读取下一行

mode的详细参数

Python通过创建文件对象,进行磁盘文件的读写(IO)。

主要函数:def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)

file:指的是文件路径

mode:读写模式,单个的模式主要有:

buffering:缓存模式

encoding:编码格式

newline:要是针对不同操作系统的换行符不一致产生的策略

部分参数解释:

mode:(可组合)

    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================

buffering:

    buffering is an optional integer used to set the buffering policy.
    Pass 0 to switch buffering off (only allowed in binary mode), 1 to select
    line buffering (only usable in text mode), and an integer > 1 to indicate
    the size of a fixed-size chunk buffer.  When no buffering argument is
    given, the default buffering policy works as follows:
    
    * Binary files are buffered in fixed-size chunks; the size of the buffer
      is chosen using a heuristic trying to determine the underlying device's
      "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
      On many systems, the buffer will typically be 4096 or 8192 bytes long.
    
    * "Interactive" text files (files for which isatty() returns True)
      use line buffering.  Other text files use the policy described above
      for binary files.

newline:

    newline controls how universal newlines works (it only applies to text
    mode). It can be None, '', '
', '
', and '
'.  It works as
    follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '
', '
', or '
', and
      these are translated into '
' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '
' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '
', no translation takes place. If newline is any
      of the other legal values, any '
' characters written are translated
      to the given string.

具体示例:

1. 首先建立文件如下,使用utf-8编码:打开原txt-->输入文本-->另存为utf-8-->覆盖原txt 【将文件设置为utf-8编码格式

2. UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 54: illegal multibyte sequence

出现这个错误时,一般是因为encoding未设置造成,例如:

f1 = open(path, 'r')
a = f1.read()    #read()一次读取全部内容,数据量很大时建议使用readline或者read(1024)等,1024表示字节数
# UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 54: illegal multibyte sequence
print(a)
f1.close()

解决:

f2 = open(path, 'r', encoding='utf-8')
a = f2.read()    #read()一次读取全部内容,数据量很大时建议使用readline或者read(1024)等,1024表示字节数
print(a)
f2.close()

3. f.read() 和 f.read(size)

f.read():一次读取整个文件

f.read(size):一次读取size字节大小的数据

# ------------------------------
f2 = open(path, 'r', encoding='utf-8')
a = f2.read()    #read()一次读取全部内容,数据量很大时建议使用readline或者read(1024)等,1024表示字节数
print(a)
f2.close()

# ------------------------------
f3 = open(path, 'r', encoding='utf-8')
a = f3.read(4)    #数据量很大时建议使用readline或者read(size)等,size表示字节数
print(a)
f3.close()

4. f.readline() 和 f.readlines()

f.readline():每次读取一行,注意文件指针的位置

f.readlines():一次读取多行,构成list

# ------------------------------
f4 = open(path, 'r', encoding='utf-8')
a = f4.readline()    #readline()一次读取一行
print(a)
f4.close()


# -----------------------------
f5 = open(path, 'r', encoding='utf-8')
a = f5.readlines()    #readlines()一次读取多行,构成list,可以用于迭代
print('a:',a)
f5.close()

5. f.tell():返回文件指针的位置,注意换行符

f7 = open(path, 'r', encoding='utf-8')
a = f7.readline()    #readlines()一次读取多行,构成list,可以用于迭代
print(a)
cc = f7.tell()   # 返回文件当前位置
print(cc)
f7.close()

6. f.writelines():向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。

r+:文件指针从开头开始读写

w+:打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。

f8 = open(path, 'r+', encoding='utf-8')   # r+文件指针从开头开始读写
a = f8.readlines()         #readlines()一次读取多行,构成list,可以用于迭代
print('a:',a)
f8.writelines(['
陈王昔时宴平乐,斗酒十千恣戏虐
','哎,李白'])   # 向文件写入一个序列字符串列表,此时文件指针在结尾处,相当于追加了
dd = f8.read()
print('dd:',dd)  #此时文件指针在末尾,所以为空
f8.close()

7. f.write():写入单个字符串

f9 = open(path, 'r+', encoding='utf-8') # r+文件指针从开头开始读写
a = f9.readlines()    #readlines()一次读取多行,构成list,可以用于迭代
print('a:',a)
f9.write('
陈王昔时宴平乐,斗酒十千恣戏虐') #通过上述读操作,文件指针在结尾处;此时写操作也会在结尾处进行
b = f9.read()
print('b:',b)  #为空,因为文件指针的位置
f9.close()

8. f.seek():用于设置文件指针的位置 —— f.seek(偏移量,[起始位置])

a+:打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。

f10 = open(path, 'r', encoding='utf-8')
a = f10.readlines()    #readlines()一次读取多行,构成list,可以用于迭代
print('a:',a)
f10.close()
f11 = open(path, 'a+', encoding='utf-8')   #a+模式用于追加
f11.write('
陈王昔时宴平乐,斗酒十千恣戏虐')
b = f11.read()
print('b:',b)    #这里值为空,是因为文件指针此时在文件的末尾;如果要读数据,需要设置文件指针的位置
f11.seek(6,0)  # f.seek(偏移量,[起始位置]),用来移动文件指针;偏移量: 单位为比特(字节),可正可负;起始位置: 0-文件头, 默认值; 1-当前位置; 2-文件尾
b = f11.read()  
print('b:',b)
f11.close()

9. 文件指针

# 文件指针
f12 = open(path, 'a+', encoding='utf-8')   #a+模式用于追加
f12.write('
陈王昔时宴平乐,斗酒十千恣戏虐')
c = f12.tell()   # 返回文件当前位置,因为a+,所以上述的write操作在末尾进行,然后文件指针也在末尾了:269
print('c:',c)
f12.seek(6,0)  # f.seek(偏移量,[起始位置]),用来移动文件指针;偏移量: 单位为比特(字节),可正可负;起始位置: 0-文件头, 默认值; 1-当前位置; 2-文件尾
d = f12.tell()   # 返回文件当前位置,经过偏移之后为:6
print('d:',d)
e = f12.read()  #读取整个文件,读完后又到末尾了
print('e:',e)
f = f12.tell()   # 返回文件当前位置,此时在末尾:269
print('f:',f)
f12.close()

10. with open:可以不需要显式关闭文件操作:f.close()

f.__next__():读取下一行

# ------------------------------------
# 2. with open:可以不需要显式关闭文件操作:f.close
with open(path, 'r', encoding='utf-8') as f13:
    a = f13.readlines()    #readline()一次读取一行
    print(a)

# 下一行:f.__next__()
with open(path, 'r', encoding='utf-8') as f13:
    a = f13.readline()    #readline()一次读取一行
    print('a:',a)
    b = f13.__next__()
    print('b:',b)

 11. mode的详细参数(来自菜鸟教程):

模式描述
t 文本模式 (默认)。
x 写模式,新建一个文件,如果该文件已存在则会报错。
b 二进制模式。
+ 打开一个文件进行更新(可读可写)。
U 通用换行模式(不推荐)。
r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。
w 打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
w+ 打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

默认为文本模式,如果要以二进制模式打开,加上 b 。

参考:

https://www.cnblogs.com/mengyu/p/6638975.html

https://www.runoob.com/python/file-methods.html

原文地址:https://www.cnblogs.com/qi-yuan-008/p/12916170.html