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编码

更多Python知识请关注云海天Python教程栏目。

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » python读取文本内容中文乱码怎么解决