def unicode_to_str(unicode_str):
return unicode_str.encode().decode('unicode_escape')
def str_to_unicode(string):
new_str = ''
for ch in string:
if '\u4e00' <= ch <= '\u9fff':
new_str += hex(ord(ch)).replace('0x', '\\u')
else:
new_str += ch
return new_str
if __name__ == '__main__':
unicode = str_to_unicode('你好')
print(unicode) # \u4f60\u597d
print(repr(unicode)) # '\\u4f60\\u597d'
print(unicode_to_str('\\u4f60\\u597d')) # 你好