ITPub博客

首页 > IT职业 > IT职场 > python读取文本内容中文乱码怎么解决

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

IT职场 作者:w39 时间:2021-09-11 10:17:04 0 删除 编辑

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视频教程栏目。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/854/viewspace-2833705/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-14

  • 博文量
    92
  • 访问量
    268566