打印包含重音字符和表情符号的 Unicode 字符串
Print Unicode string containing both accented characters and emoticons
我正在读取 Python 的文件,其中恰好包含以下行
à è ì ò ù ç @ \U0001F914
其中 \U0001F914
是表情符号的 unicode 代码。
如果将字符串解释为
string=string.decode('utf-8')
我得到:
à è ì ò ù ç @ \U0001F914
而如果我解释如下:
string=string.decode('unicode-escape')
我得到:
à è ì ò ù ç @
我该如何打印:
à è ì ò ù ç @
我是初学者,如果我的问题很愚蠢,请原谅我,但我无法解决。
提前致谢。
\U0001F914 超出了 IDLE、Tk 和大多数终端的可打印范围。
也许这不是最好的解决方案,但首先你可以使用 encode
和 'unicode-escape'
而不是 decode
,你会得到
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape')
\xe0 \xe8 \xec \xf2 \xf9 \xe7 @ \U0001F914
然后您必须将 \
替换为 \
- 在 Python 中您将需要 \\
和 \
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape').replace('\\', '\')
\xe0 \xe8 \xec \xf2 \xf9 \xe7 @ \U0001F914
然后您可以将 decode
与 'unicode-escape'
一起使用
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape').replace('\\', '\').decode('unicode-escape')
à è ì ò ù ç @
编辑:
看来你得在开头加上.decode('utf-8')
#-*- coding: utf-8 -*-
data = 'à è ì ò ù ç @ \U0001F914'.decode('utf-8')
result = data.encode('unicode-escape').replace('\\', '\').decode('unicode-escape')
print result #.encode('utf-8')
我正在读取 Python 的文件,其中恰好包含以下行
à è ì ò ù ç @ \U0001F914
其中 \U0001F914
是表情符号的 unicode 代码。
如果将字符串解释为
string=string.decode('utf-8')
我得到:
à è ì ò ù ç @ \U0001F914
而如果我解释如下:
string=string.decode('unicode-escape')
我得到:
à è ì ò ù ç @
我该如何打印:
à è ì ò ù ç @
我是初学者,如果我的问题很愚蠢,请原谅我,但我无法解决。
提前致谢。
\U0001F914 超出了 IDLE、Tk 和大多数终端的可打印范围。
也许这不是最好的解决方案,但首先你可以使用 encode
和 'unicode-escape'
而不是 decode
,你会得到
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape')
\xe0 \xe8 \xec \xf2 \xf9 \xe7 @ \U0001F914
然后您必须将 \
替换为 \
- 在 Python 中您将需要 \\
和 \
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape').replace('\\', '\')
\xe0 \xe8 \xec \xf2 \xf9 \xe7 @ \U0001F914
然后您可以将 decode
与 'unicode-escape'
data = 'à è ì ò ù ç @ \U0001F914'
print data.encode('unicode-escape').replace('\\', '\').decode('unicode-escape')
à è ì ò ù ç @
编辑:
看来你得在开头加上.decode('utf-8')
#-*- coding: utf-8 -*-
data = 'à è ì ò ù ç @ \U0001F914'.decode('utf-8')
result = data.encode('unicode-escape').replace('\\', '\').decode('unicode-escape')
print result #.encode('utf-8')