encode(编码)   decode(解码)   encoding(编码格式)

#-*- coding:utf-8 -*-
import chardet #用于查看编码 with open("d:/kk/kk.txt", "rb") as temp:
tem = temp.read()
print(tem)
print(chardet.detect(tem))
print(tem.decode(encoding="gb2312"))
print((tem.decode(encoding="gb2312")).encode("utf-8"))
print(chardet.detect((tem.decode(encoding="gb2312")).encode("utf-8")))

结果:

b'\xc4\xe3\xba\xc3\r\n\xc4\xe3\xba\xc3\r\n\xc4\xe3\xba\xc3'
{'encoding': 'TIS-620', 'confidence': 0.3598212120361634, 'language': 'Thai'}
你好
你好
你好
b'\xe4\xbd\xa0\xe5\xa5\xbd\r\n\xe4\xbd\xa0\xe5\xa5\xbd\r\n\xe4\xbd\xa0\xe5\xa5\xbd'
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}

其它编码转换:

aa = "\\u672c\\u7248\\u672c\\u5185\\u4e0d\\u652f\\u6301\\u7684\\u63a5\\u53e3\\u6216\\u8005\\u63a5\\u53e3\\u5df2\\u7ecf\\u88ab\\u5e9f\\u5f03\\uff0c\\u8bf7\\u53c2\\u8003\\u8be5\\u63a5\\u53e3\\u7684\\u6587\\u6863\\u3002"
print(type(aa))
print(aa.encode('utf-8').decode('unicode_escape'))

python读取文件编码转换问题的更多相关文章

  1. Python读取文件编码及内容

    Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...

  2. Python读取文件编码解码问题

    用chardet检测编码 import chardet raw = open("model.json", 'rb').read() result = chardet.detect( ...

  3. Python 读取文件中unicode编码转成中文显示问题

    Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...

  4. 使用java进行文件编码转换

    在开发过程中,可能会遇到文件编码的转换,尽管说开发工具eclipse能够转换编码,可是有的情况却非常不方便.比方,原来文件本身的编码是GBK,如今要转换成UTF-8,假设直接在eclipse中把文件编 ...

  5. 解决 python 读取文件乱码问题(UnicodeDecodeError)

    解决 python 读取文件乱码问题(UnicodeDecodeError) 确定你的文件的编码,下面的代码将以'utf-8'为例,否则会忽略编码错误导致输出乱码 解决方案一 with open(r' ...

  6. windows linux 文件编码转换

    查看文件编码在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vi ...

  7. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

  8. 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence

    python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...

  9. [转载]Linux查看文件编码格式及文件编码转换

    原文地址:https://blog.csdn.net/gatieme/article/details/55045883 3 linux下查看编码的方法 方法一:file filename file c ...

随机推荐

  1. 剑指 Offer 61. 扑克牌中的顺子

    剑指 Offer 61. 扑克牌中的顺子 从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的.2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王为 0 ,可以看成任意 ...

  2. 使用VSCode创建第一个VUE项目

    vue init webpack vue_test回车,然后输入工程名称vue_test vue:Missing space before value for key 'components' 原因是 ...

  3. C# Unity容器的使用

    最简单的使用方式(记得安装Unity NuGet包呀) Console.WriteLine("***************Unity容器的初步应用***************" ...

  4. 客户机与服务器TCP连接状态

    客户机:

  5. Hibernate之抓取策略

    时间:2017-1-23 19:08 --区分延迟和立即检索1.立即检索    当执行某行代码时,会马上发出SQL语句进行查询.    例如:get()2.延迟检索    当执行某行代码时,不会马上发 ...

  6. String与Int类型的转换

    http://blog.sina.com.cn/s/blog_4f9d6b1001000bfo.html int -> String int i=12345; String s="&q ...

  7. 刷题-力扣-213. 打家劫舍 II

    213. 打家劫舍 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/house-robber-ii/ 著作权归领扣网络所有.商业 ...

  8. spring支持的Bean的作用域

    Sigleton:单例模式,在整个Spring IoC容器中,使用Sigleton定义Bean将有一个实例 prototype:原型模式,每次通过容器的getBean方法获取propertype都将产 ...

  9. promise例题

    let promise = new Promise(resolve => { console.log('Promise'); resolve(); }); promise.then(functi ...

  10. springboot系列总结(一)---初识springboot

    Spring Boot是一个简化Spring开发的框架.用来监护spring应用开发,约定大于配置,去繁就简,just run 就能创建一个独立的,产品级的应用. 一说springboot ,Java ...