python读取文本内容中文乱码怎么解决
发布时间:2026-04-09 15:34:29

python阅读文本内容乱码的解决方案:
1、查看文件编码方法:
importchardet fobj=open(fname,'r') data=fobj.read() printchardet.detect(data)['encoding']
2、编码类型转换
python默认使用unicode字符集,utf-8.
str.decode('gbk')#将gbk编码字符串转换为unicode编码
str.encode('gbk')#将unicode编码的字符串转换为gbk编码请关注Python视频教程栏目,了解更多Python知识。
下一篇 python汉字不能编译怎么解决
