Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
phenomenon
Print any object containing Chinese, dictionaries, lists, DataFrame, or string. such as:
print ( 'Chinese')
Console error:
Traceback (most recent call last): File "printcn.py", line 1, in <module> print('\u4e2d\u6587') UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
For the other machine can properly display the Chinese. Or execution can also be displayed in the normal PyCharm. Only the command line console error.
My environment is MacOS 10.13.3 Chinese, Anaconda3 5.0.1
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 6 2017, 12:04:38) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Find out why
If you need a python 2.X, then add in the file # - * - coding: utf8 - * -, and reload (sys) sys.setdefaultencoding ( "utf8 "). But Python3 should default on the use utf8 encoding, and even set these still can not print properly.
Some people say that function solved by encode ( 'utf-8'), but if the dictionary or print directly DataFrame, each element can not encode general bar.
Finally see a bit coding system environment
>>> import sys >>> sys.stdout.encoding 'US-ASCII'
And another printing machine is normally en_US.UTF-8
Solution
(1) set the environment variable LANG
As set environment variables in linux or Mac mode, edit ~ / .bash_profile file ( '~' refers to the default directory after the user logs on), add the line:
export LANG="en_US.UTF-8"
Re-open a command line console Save and exit
(2) PYTHONIOENCODING
Add parameters before running the python command PYTHONIOENCODING = utf-8 python printcn.py
Explanation of this parameter can check out the official document: https://docs.python.org/3.6/using/cmdline.html#envvar-PYTHONIOENCODING
(3) redefine the standard output
Add code sys.stdout = codecs.getwriter ( "utf-8 ") (sys.stdout.detach ()), so the code becomes:
import sys import codecs sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) print('中文')
phenomenon
Print any object containing Chinese, dictionaries, lists, DataFrame, or string. such as:
print ( 'Chinese')
Console error:
Traceback (most recent call last): File "printcn.py", line 1, in <module> print('\u4e2d\u6587') UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
For the other machine can properly display the Chinese. Or execution can also be displayed in the normal PyCharm. Only the command line console error.
My environment is MacOS 10.13.3 Chinese, Anaconda3 5.0.1
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 6 2017, 12:04:38) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Find out why
If you need a python 2.X, then add in the file # - * - coding: utf8 - * -, and reload (sys) sys.setdefaultencoding ( "utf8 "). But Python3 should default on the use utf8 encoding, and even set these still can not print properly.
Some people say that function solved by encode ( 'utf-8'), but if the dictionary or print directly DataFrame, each element can not encode general bar.
Finally see a bit coding system environment
>>> import sys >>> sys.stdout.encoding 'US-ASCII'
And another printing machine is normally en_US.UTF-8
Solution
(1) set the environment variable LANG
As set environment variables in linux or Mac mode, edit ~ / .bash_profile file ( '~' refers to the default directory after the user logs on), add the line:
export LANG="en_US.UTF-8"
Re-open a command line console Save and exit
(2) PYTHONIOENCODING
Add parameters before running the python command PYTHONIOENCODING = utf-8 python printcn.py
Explanation of this parameter can check out the official document: https://docs.python.org/3.6/using/cmdline.html#envvar-PYTHONIOENCODING
(3) redefine the standard output
Add code sys.stdout = codecs.getwriter ( "utf-8 ") (sys.stdout.detach ()), so the code becomes:
import sys import codecs sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) print('中文')