使用PyCharm读取Walden.txt内容时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence错误,查了一下是文件编码机制的问题,意思是当内部编码转化成 gbk编码(默认)时出错。
例如,错误代码是:
path = 'C:/Users/Administrator/Desktop/Walden.txt'
with open(path,'r') as text:
加一个pycharm支持的编码就可,选择的是 utf-8,即修改后的代码是:
path = 'C:/Users/Administrator/Desktop/Walden.txt'
with open(path,'r',encoding='UTF-8') as text: