lududuul 发表于 2020-8-18 17:28:28

python 文件 问题

>>> f = open('F:\\111.txt')
>>> print(f.read())
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
    print(f.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 19: illegal multibyte sequence
>>>



自己写了一个带有中文的文本文件,为什么会报错?

Twilight6 发表于 2020-8-18 18:03:33

编码错误,这样试试:

f = open('F:\\111.txt',encoding='utf-8')
print(f.read())

如果还是编码报错,那么你重新另存为下 txt 文本即可:

https://xxx.ilovefishc.com/forum/202005/25/072647nvzjv2zemvgxet5g.jpg
选择 utf-8 编码保存时候,需要 open 加上 encoding = 'utf-8' 参数
https://xxx.ilovefishc.com/forum/202005/25/072650zqq1fqqmng421bqz.jpg
选择 ANSI 编码保存,可以不加上参数直接运行
https://xxx.ilovefishc.com/forum/202006/22/222937li4vv400i6v44b6z.png

页: [1]
查看完整版本: python 文件 问题