Home Backend Development Python Tutorial Detailed explanation of the usage of function setdefaultencoding

Detailed explanation of the usage of function setdefaultencoding

Aug 21, 2017 pm 02:55 PM
usage Detailed explanation

sys.getdefaultencoding() sets the default string encoding format. If you do not specify the encoding method when encoding and decoding in python, then python will use defaultencoding.

The default encoding of python2.x is ascii, which is why most python encoding errors: "UnicodeDecodeError: 'ascii' codec can't decode byte...".

# coding:utf-8, which has a similar function, is used to define the encoding of the source code. If it is not defined, the source code cannot contain Chinese strings.

Note: Setdefaultencoding has been abandoned since python2.7, so it cannot be used in python3. ##When calling setdefaultencoding, you must first reload the sys module, because the import statement here is not actually the first import statement of sys, which means that this may actually be the second or third import of the sys module. Here is just A reference to sys can only be reloaded by reload.

So why does it need to be reloaded, but the function cannot be called if it is directly referenced? Because the setdefaultencoding function is deleted after being called by the system, it is no longer there when it is referenced through import. Therefore, the sys module must be reloaded once so that setdefaultencoding will be available and the current character encoding of the interpreter can be modified in the code.

In the Lib folder of the python installation directory, there is a file called site.py, in which you can find main() --> setencoding()-->sys.setdefaultencoding(encoding), Because this site.py is automatically loaded every time the python interpreter is started, the main function will be executed every time, and the setdefaultencoding function has been deleted as soon as it comes out.

The above is the detailed content of Detailed explanation of the usage of function setdefaultencoding. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Detailed explanation of obtaining administrator rights in Win11

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in Oracle SQL

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to correctly use the exit function in C language

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

Usage of WPSdatedif function

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

Detailed explanation of the role and usage of PHP modulo operator

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of the linux system call system() function

Detailed analysis of C language learning route Detailed analysis of C language learning route Feb 18, 2024 am 10:38 AM

Detailed analysis of C language learning route

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcuts

See all articles