在Python的控制台中显示表情符号 [英] Display Emoji in Python's console
问题描述
我想知道是否可以在Windows的Python 3控制台中打印表情符号。实际上,为避免发生以下错误:
I wonder if it's possible to print Emojis in a Python 3 console on Windows. Actually, to avoid the following error:
codec can't encode character '\U0001f44d' in position 10: character maps to
<undefined>
我用过:
import emoji as moji
print(moji.emojize('Python is :thumbsup:', use_aliases=True).encode('unicode-
escape'))
即预期,打印正确的字符:U0001f44d
没有任何例外
。
which is, as expected, printing the right character:U0001f44d
without any exception
.
推荐答案
Windows命令提示符在Unicode字符方面有很多限制,尤其是在基本多语言平面(BMP或U + 0000到U + FFFF)之外的字符。命令提示符默认为旧式OEM编码(在US Windows上为cp437),并且对本地化编码之外的字符的字体支持有限。找到一个对UTF-8有良好支持的Python IDE。
The Windows command prompt has a lot of limitations with regards to Unicode characters, especially those outside the basic multilingual plane(BMP, or U+0000 to U+FFFF). The command prompt defaults to a legacy OEM encoding (cp437 on US Windows) and has limited font support for characters outside the localized encoding. Find a Python IDE that has good support for UTF-8.
一种查看各种Unicode字符的快捷方法是写入文件,然后利用浏览器:
One quick-and-dirty way to see a wide variety of Unicode characters is to write to a file and leverage the browser:
import os
with open('test.htm','w',encoding='utf-8-sig') as f:
f.write('\U0001f44d')
os.startfile('test.htm')
这将显示在Windows 10系统上的最新Chrome浏览器中。
This displays in the latest Chrome browser on my Windows 10 system.
这篇关于在Python的控制台中显示表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!