您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python从文件读取并保存到utf-8

Python从文件读取并保存到utf-8

使用以下codecs模块在程序的I / O边界处处理与Unicode之间的文本:

import codecs
with codecs.open(filename, 'r', encoding='utf8') as f:
    text = f.read()
# process Unicode text
with codecs.open(filename, 'w', encoding='utf8') as f:
    f.write(text)

io现在建议使用该模块代替编解码器,并且该模块与Python 3的open语法兼容,如果使用Python 3,则可以在open不需要Python 2兼容性的情况下使用。

import io
with io.open(filename, 'r', encoding='utf8') as f:
    text = f.read()
# process Unicode text
with io.open(filename, 'w', encoding='utf8') as f:
    f.write(text)
python 2022/1/1 18:28:27 有190人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶