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

图片[1]-python读取文本内容中文乱码怎么解决-uusu优素-乐高,模型,3d打印,编程

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编码
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容