检查文件的编码格式:将文件保存为UTF-8格式或GB18030格式。
使用Python的codecs模块进行解码和编码操作:
import codecs
with codecs.open("filename.txt", "r", "utf-8") as f:
data = f.read()
with codecs.open("filename.txt", "w", "utf-8") as f:
f.write(data)
使用Python的io模块进行解码和编码操作:
import io
with io.open("filename.txt", "r", encoding="utf-8") as f:
data = f.read()
with io.open("filename.txt", "w", encoding="utf-8") as f:
f.write(data)