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' # 好的
If I understand correctly, the file contains the literal text
\u65b9\u6cd5\uff0c\u5220\u9664\u5b58\u50a8\u5728
(so it's plain ASCII, but with backslashes and all that describe the Unicode ordinals the same way you would in a Pythonstr
literal). If so, there are two ways to handle this:mystr = mybytes.decode('unicode-escape')
to convert from thebytes
tostr
interpreting the escapescodecs
module for the "text -> text" conversion (bytes to bytes and text to text codecs are now supported only by thecodecs
module functions;bytes.decode
is purely for bytes to text andstr.encode
is purely for text to bytes, because usually, in Py2,str.encode
andunicode.decode
was a mistake, and removing the dangerous methods makes it easier to understand what direction the conversions are supposed to go), e.g.decodedstr = codecs.decode(encodedstr, 'unicode-escape')