当前位置: 首页 > 图灵资讯 > 行业资讯> python读取文本内容中文乱码怎么解决

python读取文本内容中文乱码怎么解决

来源:图灵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知识。