python读文件出现错误解决方法

python读文件经常会出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbd in position 764: illegal multibyte sequence

解决方法:
以"rb",方式读再以一定的方式解码,如"UTF-8","UTF-16", "gbk"

with open(log_path, 'rb') as fd:
        lines = [l.decode('utf8', 'ignore') for l in fd.readlines()]         

猜你喜欢

转载自www.cnblogs.com/michaelcjl/p/11884927.html
今日推荐