python3字符编码转换_Python3 中文字符与Unicode字符互转(原创)

中文转Unicode

def cn_to_unicode(in_str, need_str=True, debug=False):

out = []

for s in in_str:

# 获得该字符的数值

val = ord(s)

# print(val)

# 小于0xff则为ASCII码,手动构造\u00xx格式

if val <= 0xff:

hex_str = hex(val).replace('0x', '').zfill(4)

# 这里不能以unicode_escape编码,不然会自动增加一个'\\'

res = bytes('\\u' + hex_str, encoding='utf-8')

else:

res = s.encode("unicode_escape")

out.append(res)

# 调试

if debug:

print(out)

print(len(out), len(out[0]), len(out[-1]))

# 转换为str类

if need_str:

out_str = ''

for s in out:

out_str += str(s, encoding='utf-8')

return out_str

else:

return out

Unicode转中文

def unicode_to_cn(in_str, debug=False):

out = None

if isinstance(in_str, bytes):

temp = str(in_str, encoding='utf-8')

out = temp.encode('utf-8').decode('unicode_escape')

else:

out = in_str.encode('utf-8').decode('unicode_escape')

return out

测试代码

test.py

...

if __name__ == "__main__":

val = input("unicode to GBK or GBK to unicode? : ")

if eval(val) == 1:

s1 = input("input unicode str(like '\\u4f60\\u597d'): ")

s2 = unicode_to_cn(s1)

print("result: ", s2)

elif eval(val) == 2:

s1 = input("input GBK str(like '你好'): ")

s2 = cn_to_unicode(s1)

print("result: ", s2)

else:

print("input wrong choice! can only be 1 or 2!")

执行效果

unicode to 中文

jason@jason-vm:~/test$ python3 test.py

unicode to GBK or GBK to unicode? : 1

input unicode str(like '\u4f60\u597d'): \u4f60\u597d

result: 你好

中文 to unicode

jason@jason-vm:~/test$ python3 test.py

unicode to GBK or GBK to unicode? : 2

input GBK str(like '你好'): 你好123

result: \u4f60\u597d\u0031\u0032\u0033

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值