python读取含有中文的txt文件问题
用read命令打开含有中文的txt文件出现如下错误:>>> f = open(r'test.txt')
>>> f.read()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 34: illegal multibyte sequence
>>>
怎样彻底解决这种问题,英文好像不会发生。谢谢! 本帖最后由 Twilight6 于 2020-6-17 10:29 编辑
f = open(r'test.txt',encoding = 'utf-8')
如果懒得在 open 函数里面 加 encoding = 'utf-8' 的话就这样操作吧
重新另存为设置下txt文件的编码:
第一步:
https://xxx.ilovefishc.com/forum/202005/25/072650zqq1fqqmng421bqz.jpg
第二步:
https://xxx.ilovefishc.com/forum/202005/25/072647nvzjv2zemvgxet5g.jpg
第三步:
设置为 ANSI 编码即可直接 read()
为啥英文不报错,因为GBK编码和utf-8编码里面都有英文对照字符。
而中文没有,所以报错了。
要么 f = open(r'test.txt',encoding ="utf-8")
要么存和取格式一致都是utf-8. Twilight6 发表于 2020-6-17 10:22
如果懒得在 open 函数里面 加 encoding = 'utf-8' 的话就这样操作吧
重新另存为设置下txt文件的编码 ...
明白啦,感谢。 glacierjin 发表于 2020-6-17 15:06
明白啦,感谢。
客气客气~ heidern0612 发表于 2020-6-17 10:27
为啥英文不报错,因为GBK编码和utf-8编码里面都有英文对照字符。
而中文没有,所以报错了。
谢谢!
页:
[1]