SoFunction
Updated on 2025-03-02

Default encoding usage in python

python default encoding

In python2, the default ASCII encoding is used.

The difference between this encoding and the beginning encoding is thatThe encoding at the beginning is the encoding for the file content. The default encoding is the encoding used by default in some python methods.

For example, when encode str, decode encoding is first used by default, such as encode encoding of file write operation, etc.

UTF-8 encoding is used by default in python3

() Get the default encoding

import sys
print(())  
  • python2

D:\SoftInstall\Python\Python38\ E:/PycharmProjects/displayPY3/
ascii

Process finished with exit code 0

  • python3

D:\SoftInstall\Python\Python38\ E:/PycharmProjects/displayPY3/
utf-8

Process finished with exit code 0

(Coding format) Modify the default encoding

The following example is used in python2 environment

# coding=utf-8
import sys
print(())
reload(sys)
('utf-8')
print(())
s = 'Chinese'
('utf-8')
print(s)
#Equivalent to(“utf-8”).encode('utf8')

E:\PycharmProjects\LEDdisplay2\venv\Scripts\ E:/PycharmProjects/LEDdisplay2/
ascii
utf-8
Chinese

If the above code does not modify the default encoding, the default encoding ASCII will be used to decode the variable s, and an error will be reported

# coding=utf-8
import sys
print(())
s = 'Chinese'
('utf-8')
print(s)

E:\PycharmProjects\LEDdisplay2\venv\Scripts\ E:/PycharmProjects/LEDdisplay2/
ascii
Traceback (most recent call last):
  File "E:/PycharmProjects/LEDdisplay2/", line 5, in <module>
    ('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

Process finished with exit code 1

Why do the above code need reload(sys)?

Please see the following code:

# coding=utf-8
import sys
print(())
# reload(sys)
('utf-8')
print(())
s = 'Chinese'
('utf-8')
print(s)

E:\PycharmProjects\LEDdisplay2\venv\Scripts\ E:/PycharmProjects/LEDdisplay2/
Traceback (most recent call last):
  File "E:/PycharmProjects/LEDdisplay2/", line 5, in <module>
    ('utf-8')
AttributeError: 'module' object has no attribute 'setdefaultencoding'
ascii

Process finished with exit code 1

reload is a module used to reload previous imports.

The reason why sys needs to be reloaded here is:

Python deleted the setdefaultencoding method in sys when loading the module (maybe for security reasons), so it is necessary to reload the sys module.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.