windows下open函数'gbk' codec can't decode byte

windows下open函数’gbk’ codec can’t decode byte

lyg
lyg
这是由于在Windows下Python使用open()函数打开文件时会默认使用gbk解码,即使文件本身存储为UTF-8格式。

将:

Python

with open(‘temp.txt’, ‘r’) as f:
改为:

Python

with open(‘temp.txt’,‘r’,encoding=‘utf-8’) as f:
即指定以UTF-8解码方式打开此文件,这样就避免了解码错误。

https://www.polarxiong.com/archives/python-3-open-file-gbk-error.html
https://zhuanlan.zhihu.com/p/33185846

猜你喜欢

转载自blog.csdn.net/shuidefu/article/details/89289750
今日推荐