kaw*_*aze 3 python unicode python-2.7
>>> test
u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old\xe2"'
>>> test2
'"Hello," he\\u200b said\\u200f\\u200e.\n\t"I\\u200b am\\u200b nine years old"'
>>> print test
"Hello," he said??.
"I am nine years oldâ"
>>> print test2
"Hello," he\u200b said\u200f\u200e.
"I\u200b am\u200b nine years old"
Run Code Online (Sandbox Code Playgroud)
那么我将如何从 test2 转换为 test(即打印 unicode 字符)?.decode('utf-8')
不这样做。
您可以使用unicode-escape
编码解码'\\u200b'
为u'\u200b'
.
>>> test1 = u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old\xe2"'
>>> test2 = '"Hello," he\\u200b said\\u200f\\u200e.\n\t"I\\u200b am\\u200b nine years old"'
>>> test2.decode('unicode-escape')
u'"Hello," he\u200b said\u200f\u200e.\n\t"I\u200b am\u200b nine years old"'
>>> print test2.decode('unicode-escape')
"Hello," he? said??.
"I? am? nine years old"
Run Code Online (Sandbox Code Playgroud)
注意:但即使这样,test2
也无法解码以完全匹配,test1
因为在结束引号 ( )之前有一个u'\xe2'
in 。test1
"
>>> test1 == test2.decode('unicode-escape')
False
>>> test1.replace(u'\xe2', '') == test2.decode('unicode-escape')
True
Run Code Online (Sandbox Code Playgroud)