美文网首页
python读取ANSI文本乱码问题

python读取ANSI文本乱码问题

作者: 不爱吃饭的小孩怎么办 | 来源:发表于2019-07-17 09:08 被阅读0次

文本文件是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

相关文章

网友评论

      本文标题:python读取ANSI文本乱码问题

      本文链接:https://www.haomeiwen.com/subject/rbuflctx.html