这是由于在Windows下Python使用open()
函数打开文件时会默认使用gbk
解码,即使文件本身存储为UTF-8
格式。
将:
with open('temp.txt', 'r') as f:
改为:
with open('temp.txt','r',encoding='utf-8') as f:
即指定以UTF-8
解码方式打开此文件,这样就避免了解码错误。
这是由于在Windows下Python使用open()
函数打开文件时会默认使用gbk
解码,即使文件本身存储为UTF-8
格式。
将:
with open('temp.txt', 'r') as f:
改为:
with open('temp.txt','r',encoding='utf-8') as f:
即指定以UTF-8
解码方式打开此文件,这样就避免了解码错误。
Copyright © 2021 编程员教程