问题现象 :
with open("E:\\a\\text1.txt") as f:
for line in f:
print(line)
按行读取文本文件时报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x89 in position 14: illegal multibyte sequence
问题根因:
python的open方法默认编码取决于平台,如果是windows平台,默认编码是gbk,如果文件是utf8编码,就不会报这个错误。
解决方法:
with open("E:\\a\\text1.txt",encoding='UTF-8') as f:
for line in f:
print(line)