标签:utf str python 解码 gbk unicode print
如果想知道python 的某个bytes类型是通过什么类型编码,可以先安装chardet 。
pip install chardet
Python utf-8 编码及解码
str = "python编码" # 转为utf-8 类型的bytes 字符串 str_utf8 = str.encode("utf-8") print("转码结果:"+repr(str_utf8)) print(type(str_utf8)) print(chardet.detect(str_utf8)) print("解码结果:"+str_utf8.decode("utf-8"))
运行结果:
转码结果:b'python\xe7\xbc\x96\xe7\xa0\x81' <class 'bytes'> {'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''} 解码结果:python编码 转码结果:b'python\xb1\xe0\xc2\xeb'
Python gbk 编码及解码
# 转为gbk 类型的bytes 字符串 str_gbk = str.encode("gbk") print("转码结果:"+repr(str_gbk)) print(type(str_gbk)) print(chardet.detect(str_gbk)) print("解码结果:"+str_gbk.decode("gbk"))
运行结果:
转码结果:b'python\xb1\xe0\xc2\xeb' <class 'bytes'> {'encoding': None, 'confidence': 0.0, 'language': None} 解码结果:python编码
Python unicode 编码及解码
# 转为unicode 类型的bytes 字符串 str_unicode = str.encode("unicode-escape") print("转码结果:"+repr(str_unicode)) print(type(str_unicode)) print(chardet.detect(str_unicode)) print("解码结果:"+str_unicode.decode("unicode-escape"))
运行结果:
转码结果:b'python\\u7f16\\u7801' <class 'bytes'> {'encoding': 'ascii', 'confidence': 1.0, 'language': ''} 解码结果:python编码
标签:utf,str,python,解码,gbk,unicode,print 来源: https://www.cnblogs.com/JcHome/p/16464384.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。