def cn_to_unicode(in_str, need_str=True, debug=False):
out = []
for s in in_str:
# 獲得該字符的數(shù)值
val = ord(s)
# print(val)
# 小于0xff則為ASCII碼脖旱,手動(dòng)構(gòu)造\u00xx格式
if val <= 0xff:
hex_str = hex(val).replace('0x', '').zfill(4)
# 這里不能以u(píng)nicode_escape編碼吼鳞,不然會(huì)自動(dòng)增加一個(gè)'\\'
res = bytes('\\u' + hex_str, encoding='utf-8')
else:
res = s.encode("unicode_escape")
out.append(res)
# 調(diào)試
if debug:
print(out)
print(len(out), len(out[0]), len(out[-1]))
# 轉(zhuǎn)換為str類
if need_str:
out_str = ''
for s in out:
out_str += str(s, encoding='utf-8')
return out_str
else:
return out
Unicode轉(zhuǎn)中文
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
測(cè)試代碼
test.py
...
if __name__ == "__main__":
val = input("unicode to GBK or GBK to unicode? <enter 1 for the front, 2 for the end>: ")
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!")
執(zhí)行效果
unicode to 中文
jason@jason-vm:~/test$ python3 test.py
unicode to GBK or GBK to unicode? <enter 1 for the front, 2 for the end>: 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? <enter 1 for the front, 2 for the end>: 2
input GBK str(like '你好'): 你好123
result: \u4f60\u597d\u0031\u0032\u0033