UnicodeEncodeError: 'ascii' codec can't encode character
UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f6d1' in position 2: ordinal not in range(128)
This Python error might happen if you are having problems displaying non-ASCII characters in your terminal when running a Python script. This is mainly a problem with your locale settings used by your terminal. I provide two solutions to resolve it:
- Solution #1: change your locale settings (best solution)
- Solution #2: export PYTHONIOENCODING=utf8 (temporary solution)
Solution #1: change your locale settings (best solution)
The best solution consists in fixing your locale settings since it is permanent and you don’t have to change any Python code.
- Append ~/.bashrc or ~/.bash_profile with:
export LANG="en_US.UTF-8" export LANGUAGE="en_US:en"
You should provide your own UTF-8 based locale settings. The example uses the English (US) locale with the encoding UTF-8. The locale -a command gives you all the available locales on your Linux or Unix-like system. - Reload the .bashrc:
$ source .bashrc
- Run the locale command to make sure that your locale settings were set correctly:
$ locale LANG="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_CTYPE="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_ALL=
- Run your Python script to test if you can display the non-ASCII characters fine using the correct encoding UTF-8 :
$ python your_script.py
See also:
- How to Set Locales (i18n) On a Linux or Unix: detailed article
- How can I change the locale?: from raspberrypi.stackexchange.com, provides answers to set the locale user and system-wide
Solution #2: export PYTHONIOENCODING=utf8 (temporary solution)
Before running your Python script, export the environment variable PYTHONIOENCODING with the correct encoding:
$ export PYTHONIOENCODING=utf8 $ python your_script.py
However, this is not a permanent solution because if you use another terminal, you will have to export PYTHONIOENCODING again before running the script.
References
- These two solutions are taken from SimulRPi's documentation (a personal project of mine for which I wrote a blog post a while ago).
- Image source: www.python.org, GPL, via Wikimedia Commons
Comments
Post a Comment