解决python打开文件乱码的问题
1、已utf-8格式打开文档
f = open(r'E:\Python\liaotian.txt','r',encoding='utf-8') f.seek(0,0) for each_line in f: print(each_line) f.close()
2、以二进制打开文件,然后对读取的内容进行utf-8编码
f = open(r'E:\百度云同步盘\云同步盘\Python\liaotian.txt','rb') f.seek(0,0) for each_line in f: print(each_line.decode('utf-8')) f.close()
三、查看文档的编码格式
安装chardet模块来决绝。
推荐学习《Python教程》!