python字符串编码测试
来源:互联网 发布:身份证sql判断 编辑:程序博客网 时间:2024/05/18 10:43
#encoding=utf8import chardetdef str_detect(str): try: print ":".join("{:02x}".format(ord(c)) for c in str) t = chardet.detect(str) print t if t['encoding']=="utf-8": print str except: print type(str) pass print ""def enc(str, enc): try: s = str.encode(enc) str_detect(s) except: print "ERR:encode" def dec(str, enc): try: s = str.decode(enc) str_detect(s) except: print "ERR:decode" passprint "============================="cn = "中文"str_detect(cn)cn1 = u"中文1"str_detect(cn1)enc(cn1, 'utf-8')enc(cn1, 'utf-16')enc(cn1, 'gb2312')enc(cn1, "ISO-8859-1")cn2 = "中文2"str_detect(cn2)dec(cn2, 'utf-8')dec(cn1, 'utf-16')dec(cn1, 'gb2312')dec(cn1, "ISO-8859-1")cn3 = "中文3"dec_str = cn3.decode('utf-8')str_detect(dec_str)enc_str = dec_str.encode('utf-16')str_detect(enc_str)end_str = enc_str.decode('utf-16')str_detect(end_str)# str利用decode方法根据str的编码将其解码为unicode字符串类型# str利用encode根据特定的编码将unicode字符串类型转换为特定的编码# 注:系统不一样结果会不同
结果:
=============================e4:b8:ad:e6:96:87{'confidence': 0.7525, 'language': '', 'encoding': 'utf-8'}中文4e2d:6587:31<type 'unicode'>e4:b8:ad:e6:96:87:31{'confidence': 0.7525, 'language': '', 'encoding': 'utf-8'}中文1ff:fe:2d:4e:87:65:31:00{'confidence': 1.0, 'language': '', 'encoding': 'UTF-16'}d6:d0:ce:c4:31{'confidence': 0.682639754276994, 'language': 'Russian', 'encoding': 'KOI8-R'}ERR:encodee4:b8:ad:e6:96:87:32{'confidence': 0.7525, 'language': '', 'encoding': 'utf-8'}中文24e2d:6587:32<type 'unicode'>ERR:decodeERR:decodeERR:decode4e2d:6587:33<type 'unicode'>ff:fe:2d:4e:87:65:33:00{'confidence': 1.0, 'language': '', 'encoding': 'UTF-16'}4e2d:6587:33<type 'unicode'>
阅读全文
0 0
- python字符串编码测试
- Python字符串编码
- python字符串编码常见问题
- python字符串编码问题
- python处理字符串编码
- Python 中的字符串编码
- Python 中的字符串编码
- python字符串编码常见问题
- python字符串编码
- Python 字符串编码
- python字符串编码问题
- python字符串编码判断
- python字符串编码
- Python字符串编码
- python 字符串和编码
- Python:编码与字符串
- Python字符串编码详解
- python字符串和编码
- UnityShader从入门到放弃(一)UnityShader的结构
- React Native带你实现scrollable-tab-view(四)
- 《笨办法学python》加分习题36——我的答案
- 数据结构与算法:STL容器
- 系统大文件上传控制
- python字符串编码测试
- Educational Codeforces Round 28 C 降低复杂度的方式
- jackson工具类json转list、json转map、json转bean、bean转json
- UVA.580 Critical Mass (递推)
- 121. 单次购买股票问题
- POJ总结(2159)
- Java:利用I/O流读取文件内容
- C++ 结构体初始化
- 9. Palindrome Number