如何在Python中读写Unicode(UTF-8)文件?
现在推荐使用io模块,它与Python 3的开放语法兼容:以下代码用于在Python中读写Unicode(UTF-8)文件。
阅读更多:Python 教程
示例
import io
with io.open(filename,'r',encoding='utf8') as f:
text = f.read()
# 处理Unicode文本
with io.open(filename,'w',encoding='utf8') as f:
f.write(text)