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
复制代码

 

posted @   整合侠  阅读(328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-01-18 Pycharm创建Django项目并访问Django
2019-01-18 PyCharm 设置护眼背景色
2019-01-18 PyCharm配置Python3开发环境
2019-01-18 Python3+PyQt5+PyCharm 桌面GUI开发环境搭建
点击右上角即可分享
微信分享提示