Python读取中文文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 64
程序员文章站
2022-05-28 12:58:40
...
python读取包含中文的文件报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in position 64: illegal multibyte sequence
解决方法:打开文件时添加 encoding=’utf-8’ 进行编码即可
原程序:
with open('test.txt','r') as file:
file=file.readlines()
print(file)
for i in file:
print(i)
break
修改后:
with open('test.txt','r',encoding='utf-8') as file:
file=file.readlines()
print(file)
for i in file:
print(i)
break
推荐阅读
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
【python】pip安装报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 7: ordinal not i
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
Python读取中文文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 64
-
python3 报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position...[报错篇]
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!