后浪云Python教程:python读取文本内容中文乱码怎么解决

python读取文本内容乱码的解决方法:
1、查看文件编码方式:
import chardet fobj=open(fname,'r') data=fobj.read() print chardet.detect(data)['encoding']
2、编码类型转换
python默认使用unicode字符集,默认编码方式utf-8.
str.decode('gbk') #将gbk编码的字符串转换成unicode编码
str.encode('gbk') #将unicode编码的字符串转换成gbk编码版权声明:
作者:后浪云
链接:https://idc.net/help/182625/
文章版权归作者所有,未经允许请勿转载。
THE END
