AD

Python set the system default encoding

python is installed, the default encoding is ascii, when the program appears in non-ascii encoding, python's treatment will often report that the wrong UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range (128), python can not handle non-ascii encoding, this time need to set the python's default encoding, usually set to utf8 encoding.

Query system default encoding for the interpreter, enter the following command:

>>>sys.getdefaultencoding()

Set the default encoding to use:

>>>sys.setdefaultencoding('utf8')

May be reported AttributeError: 'module' object has no attribute 'setdefaultencoding' error, do the reload (sys), in the above command to pass.

At this point in the implementation of sys.getdefaultencoding () will find that has been set to utf8 encoding of, but in changes in the code interpreter can only be guaranteed when the second effective after restarting the interpreter, you will find they are reset code ascii as the default, then there is no way to modify the program or system once the default encoding it.

There are 2 ways to set the default python code:

A solution to the problem in the program by adding the following code:

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

Another option is the python's Lib \ site-packages folder, create a new sitecustomize.py, says:

# encoding=utf8
import sys

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

Then restart the python interpreter, the implementation of sys.getdefaultencoding (), that has been set to utf8 encoding of the many times after a reboot, the same effect, because when the system starts in python, called the document itself, set system default encoding, without the need to manually add each address code, is once and for all solution.

Another solution is involved in the program code where all the mandatory encoding utf8, which add code encode ("utf8"), this method is not recommended, because once a place to write less, will result in a large number of error report, I have encountered this situation, error log compressed still more than 70 K, all this problem, people feel there are crashes.
标签: implementation, 2 ways, attribute, lib, system default, metho, codec, python code, import sys, reboot, query system, time need, py, address code, python interpreter, code ascii
分类: Python
时间: 2011-04-26

相关文章

  1. The configuration of the system default encoding (transfer)

    Run the locale command to get the current system encoding settings in detail. 1, locale of the internal organs ...
  2. Linux system in the script to set the default encoding

    Add the following two sentences in the script, you can modify the system default encoding into UTF-8 export LC ...
  3. Modify the Eclipse / MyEclipse projects default encoding

    Chinese operating system should be the reason, eclipse the default encoding of new projects is GBK, out of con ...
  4. Modify the Eclipse / MyEclipse project's default encoding

    Chinese operating system should be the reason, eclipse the default encoding of new projects is GBK, out of con ...
  5. Change mysql default encoding

    MySQL's default encoding is Latin1, does not support Chinese, so how to modify MySQL's default encoding, Here ...
  6. Why MySQL database default encoding is Latin1

    Latin1 is ISO-8859-1 alias. Because ISO-8859-1 single-byte encoding used within the scope of all the space, in ...
  7. myeclipse modify the default encoding javascript

    Open a js file (which contains Chinese translations), edited save time, suddenly reminded of the following err ...
  8. orcale sys system default password

    sys default password: change_on_install owner of the database data dictionary system default password: manager ...
  9. eclipse settings files in the default encoding format

    eclipse all the files in the default encoding format for the iso-8859-1, easily lead to your email program, in ...
  10. UIManager user default. The appearance of the default. The system default application

    /** Print swing user default . The appearance of the default system default keys and values **/ Hashtable tabl ...
  11. Set default encoding for the mysql database gbk, address Chinese garbled

    Mysql To resolve Chinese garbled, it is best you get rid of the database's default encoding is normally such t ...
  12. linux change the system default run-level methods (transfer)

    This problem seems to be on the run level and the subject of this article has nothing to do, but between now m ...
  13. (Transfer) MyEclipse is obliged to do after the installation - such as changing the default encoding

    MyEclipse is obliged to do after the installation - such as changing the default encoding Write in the MyEclip ...
  14. mysql default encoding set gbk, address inquiries Chinese garbled dos problems under

    We use the dos window command line login. Check out the results in Chinese, there will be garbled. To solve th ...
  15. MyEclipse js default encoding

    In the Window menu to find something to Preferences, open the window on the left to find General -> Content ...
  16. mysql default encoding set gbk, to solve the query dos Chinese garbled problem

    We are using the dos window command line login. Check out the results available in Chinese, the problem will b ...
  17. [Change] Python Distributed File System Mongodb GridFS

    mongodb GridFS performance properties, net assessment is not bad. But in a production environment, in foreign ...
  18. ubuntu mysql change default encoding

    1, $ sudo cp / etc / mysql / my.cnf / etc/mysql/my.cnf_backup_2010-8-23 # backup mysql configuration file 2, $ ...
  19. Modify the default encoding

    1, need to login as root user can view the database encoding (as root landing command:> mysql-u root-p, the ...