Python读取中文文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 64

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

猜你喜欢

转载自blog.csdn.net/weixin_42074867/article/details/88551148