Python Chinese encoding setting under ubuntu

How to set python Chinese encoding under ubuntu#

The following error occurred when executing the program because Chinese characters appeared in the default encoding mode.

SyntaxError: Non-ASCII character
UnicodeDecodeError:'ascii' codec can't decode 

So change the Python encoding method to utf8 mode to adapt to Chinese characters

The following is the modification method

1. Effective in a single file###

Add it at the beginning of the Chinese-encoded file (note that it must be the beginning, there can be nothing before)

# coding: utf8

Or (this does not require the beginning)

import sys
reload(sys)
sys.setdefaultencoding('utf8')

2. Globally effective###

Modify the sitecustomize.py file in the /usr/lib/python2.7 directory and add content

# coding = utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

Explanation: Python will automatically call the file and execute the content in the file when it is started.

Additional: the way to set the default Chinese encoding in pycharm##

Open File-Default Settings-Editor-File Encodings in turn, and adjust the three encoding methods you see to utf8.

Recommended Posts

Python Chinese encoding setting under ubuntu
Install python3.6 under Ubuntu 16.04
Python MySQLd under Ubuntu
[python] python2 and python3 under ubuntu
Build a python development environment under Ubuntu
Ubuntu16 upgrade Python3
ubuntu18.04 install python2
Relearn ubuntu --python3
ubuntu12.04 install python3
ubuntu18.04 install python
ubuntu16.04 upgrade python2
Chromium is set as system setting under ubuntu 18.04
Ubuntu18.04 switch Python version
Install apache+PHP under Ubuntu
Install node.js under Ubuntu
Install mysql under Ubuntu 16.04
Install Caffe under Ubuntu 14.04
Ubuntu16.04 switch python version
Start working under ubuntu
Installation and use of GDAL in Python under Ubuntu
Use iptables under ubuntu
2018-09-11 Install arduino under Ubuntu
Install Python3 on Ubuntu 14.04
LNMP installation under Ubuntu
Network configuration under Ubuntu
Python virtual environment: Ubuntu16.04
Install python in Ubuntu
Install Python3 on Ubuntu 16.04
Install ROS under ROS Ubuntu 18.04[2]
Install MySQL under Ubuntu
Install Yarm-PM2 under Ubuntu
ubuntu set Chinese font
Install Python3.7 on Ubuntu
There is no solution for Chinese input method under ubuntu
Detailed steps for installing Django under Python 3.6 in Ubuntu 16.04 environment
Method of setting up PHP operating environment under Ubuntu server
Django&MySQL environment deployment under Ubuntu 14.04
Deploy FTP server under ubuntu
Ubuntu command line displays Chinese
Ubuntu install the latest Python 3.
Install python environment under Linux
Ubuntu 20.04 Chinese input method support
Install Python 3.7 on Ubuntu 18.04 LTS
ubuntu sets python2.5 environment variables
ubuntu18.04 compile and install python3.8
Solve Unment dependencies under ubuntu
ubuntu view python installation path
Open SSH service under Ubuntu
Use sublime tex under ubuntu
Modify shortcut keys under Ubuntu 18.04
Python script writing under SecureCRT
Install server-side Shadowsocks under Ubuntu 16.04
Open SSH service under Ubuntu
DNS service construction under Ubuntu
ubuntu build python development environment
ubuntu offline installation python environment
Install rgl package under Ubuntu