Python2.7:字符转UFT-8、GBK、BIG5并得到bytes
2022/1/18 22:06:13
本文主要是介绍Python2.7:字符转UFT-8、GBK、BIG5并得到bytes,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python2.7:字符转UFT-8、GBK、BIG5并得到bytes
# encoding: utf-8 def hexstr(s): return ''.join([hex(ord(c)).replace('0x','\\x') for c in s]) # 转big5 def toBig5(s): s1 = s.decode('utf-8') lis = [] for e in list(s1): try: lis.append(e.encode('big5')) except: lis.append('&#%d;' % ord(e)) return hexstr(''.join(lis)) # 转utf-8 def toUtf8(s): s1 = s.decode('utf-8') lis = [] for e in list(s1): lis.append(e.encode('utf-8')) return hexstr(''.join(lis)) # 转gbk def toGbk(s): s1 = s.decode('utf-8') lis = [] for e in list(s1): lis.append(e.encode('gbk')) return hexstr(''.join(lis)) # 调用入口 if __name__ == '__main__': toGbk("用户登陆") toUtf8("用户登陆") toBig5("用户登陆") #用户登陆 #utf8: \xe7\x94\xa8\xe6\x88\xb7\xe7\x99\xbb\xe9\x99\x86 #gbk: \xd3\xc3\xbb\xa7\xb5\xc7\xc2\xbd #big5:\xa5\xce\x26\x23\x32\x35\x31\x34\x33\x3b\xb5\x6e\x26\x23\x33\x38\x34\x37\x30\x3b #word38755 #utf8:\x77\x6f\x72\x64\x33\x38\x37\x35\x35 #gbk: \x77\x6f\x72\x64\x33\x38\x37\x35\x35 #big5:\x77\x6f\x72\x64\x33\x38\x37\x35\x35
这篇关于Python2.7:字符转UFT-8、GBK、BIG5并得到bytes的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python