问题描述:
使用pycharm运行如下程序时:
file = open('a.txt','r')
print(file.readlines())
file.close()
打开文件报错,显示:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal multibyte sequence
解决办法:
将你要操作的文件,即a.txt,打开,然后另存为ANSI
之后再运行程序,就可以得到正确的输出。可以看到我的txt中的内容如下:
然后运行结果如下,可以正常输出。 报错的原因是你的文件格式不对。