Display Emoji in Python's console

坚强是说给别人听的谎言 提交于 2019-12-19 08:55:18

问题


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>

I've used:

import emoji as moji
print(moji.emojize('Python is :thumbsup:', use_aliases=True).encode('unicode-
escape'))

which is, as expected, printing the right character:U0001f44d without any exception.


回答1:


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 characters outside the localized encoding. Find a Python IDE that has good support for UTF-8.

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')

This displays in the latest Chrome browser on my Windows 10 system.




回答2:


First install emoji module --- pip install emoji

import emoji
print(emoji.emojize('Python is :thumbs_up:'))

This code is working in Anaconda Jupyter environment...




回答3:


"Emoji for Python":

Install: pip install emoji --upgrade

Example:

>> import emoji
>> print(emoji.emojize('Python is :thumbs_up_sign:'))
Python is 👍
>> print(emoji.emojize('Python is :thumbsup:', use_aliases=True))
Python is 👍

You can find any emoji unicode in these links:

  1. Emoji cheat sheet
  2. Full emoji list

More information here.



来源:https://stackoverflow.com/questions/40446784/display-emoji-in-pythons-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!