python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
示例代码:
fileName = 'E:/2/采集数据_pswf12_180大0小35750_20181206.txt' currentFile = open(fileName) content = currentFile.read() print(content)
报错原因:
要打开的文件中,有‘gbk’解析不了的文本内容
那么可能是文件格式并非'gbk'格式的。
解决方法:
1.先设定编码方式打开文件
currentFile = open(fileName,encoding='gbk