文本文件是ANSI保存,以ISO-8859-1保存,Python读取报错:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 272: invalid continuation byte
修改方式如下:
with open('newFile.txt', 'r', encoding='ISO-8859-1') as fp:
for line in fp.readlines():
line = line.encode("iso-8859-1").decode('gbk')
print(line)
line为什么不直接decode?因为Python3.6的str没有decode,必须先encode后,才能decode
网友评论