python3 Chinese gibberish and default encoding format setting method

  • 2021-01-14 06:04:45
  • OfStack

The default encoding format for python is utf-8. In python2.7, the default encoding format can be set using sys.setdefaultencoding('gbk'). In python3.3, the function es8EN.setdefaultencoding () is missing. How to set the built-in default encoding format in python3.3? Urgent request!!

(Something like "#coding:gbk" is out of the question. import sys print(sys.getdefaultencoding()) output "gbk" please!!

The big difference between python 3 and 2 is that python itself is changed to unicode by default.

String no longer distinguishes "abc" from "u"abc", string "abc" is unicode by default and no longer represents local encoding,

Because of this internal encoding, such as c# and java, there is no need to encode similar Settings in the locale, such as "sys.setdefaultencoding";

As a result, python 3 code and package management breaks compatibility with 2.x. 2. The ES43en extension package should be adapted to this situation.

Another problem is how to output native encoding such as gbk with only unicode in the locale.

A: The convention is to convert the cost code only when the output is (serialized).

Such as


file.write("GBK The Chinese ".encode("GBK"))

python strings are output as byte strings using the str.encode("GBK") method for communication with other environments.


Related articles: