21.Python:指定字符编码

时间:2021-06-25 17:15:48   收藏:0   阅读:0

"""
控制文件读写内容的模式
t 文本
1.读写都是以str(unicode)为单位
2.文本文件
3.必须指定encoding=‘utf-8‘
"""

# 没有指定encoding参数操作系统会使用自己默认的编码

# with open(‘a.txt‘, mode=‘rt‘) as f1: # t模式会将f.read()读出的结果解码成Unicode
# res = f1.read()
# print(res, type(res))

with open(‘a.txt‘, mode=‘rt‘, encoding=‘utf-8‘) as f1: # t模式会将f.read()读出的结果解码成Unicode
res = f1.read()
print(res, type(res))

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!