I know this works:
a = u"\u65b9\u6cd5\uff0c\u5220\u9664\u5b58\u50a8\u5728"
print(a) # 方法,删除存储在
But if I have a string from a JSON file which does not start with "u"(a = "\u65b9\u6cd5\uff0c\u5220\u9664\u5b58\u50a8\u5728"
), I know how to make it in Python 2 (print unicode(a, encoding='unicode_escape') # Prints 方法,删除存储在
). But how to do it with Python 3?
Similarly, if it's a byte string loaded from a file, how to convert it?
print("好的".encode("utf-8")) # b'\xe5\xa5\xbd\xe7\x9a\x84'
# how to convert this?
b = '\xe5\xa5\xbd\xe7\x9a\x84' # 好的
print(a)
(your console should support unicode). To convert byte string to unicode in Python 3, usestr(b, 'utf-8')
. To test your code, use IDLE (Python shell) which supports unicode.\u65b9\u6cd5\uff0c\u5220\u9664\u5b58\u50a8\u5728
?print(str("\xe5\xa5\xbd\xe7\x9a\x84","utf-8"))
raise a error :"TypeError: decoding str is not supported",print(json.loads('"{}"'.format(b)))
, but it looks weird, if I have a very long json string and the json format is not quite right ,this method may be not work