[英]Decode unicode string in python
我想解码以下字符串:
吨\\ u028c \\ u02c8m \\ u0251 \\ u0279o \\ u028a \\ u032f
它应该是http://rhymebrain.com/talk?function=getWordInfo&word=tomorrow的JSON字符串中给出的“明天”的IPA。
我的理解是应该是这样的:
x = 't\u028c\u02c8m\u0251\u0279o\u028a\u032f'
print x.decode()
我从这里 , 这里 , 这里和这里 (以及其他或多或少适用的其他方法)尝试了解决方案,并对其部分进行了一些排列,但我无法使其正常工作。
谢谢
您需要在字符串(在似乎正在使用的Python 2.x中)之前加一个u
,以指示这是一个unicode字符串:
>>> x = u't\u028c\u02c8m\u0251\u0279o\u028a\u032f' # note the u
>>> print x
tʌˈmɑɹoʊ̯
如果已经将字符串存储在变量中,则可以使用以下构造函数将字符串转换为unicode:
>>> s = 't\u028c\u02c8m\u0251\u0279o\u028a\u032f' # your string has a unicode-escape encoding but is not unicode
>>> x = unicode(s, encoding='unicode-escape')
>>> print x
tʌˈmɑɹoʊ̯
>>> x
u't\u028c\u02c8m\u0251\u0279o\u028a\u032f' # a unicode string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.