Home  >  Article  >  Backend Development  >  python why encoding statement

python why encoding statement

coldplay.xixi
coldplay.xixiOriginal
2020-08-27 13:53:063113browse

The reason why python needs to encode the statement: 1. The first line in python is to indicate what executable program the code in this file uses to run it; 2. If you want to write it in the py file of python2 Chinese, you must add a line of comments declaring the file encoding, otherwise python2 will use ASCII encoding by default.

python why encoding statement

Related learning recommendations: python tutorial

The reason why python needs to encode the statement:

The first line in python is to point out what executable program you want the code in your file to use to run it, just So simple

#!/usr/bin/python is equivalent to hard-coding the python path (it tells the OS to call the python interpreter under /usr/bin when executing this py;)

#!/usr/bin/env python will go to the environment settings to find the python directory. This writing method is recommended (the purpose is to prevent the user from not installing python in the default /usr/ for the OS) bin path. Meaningful in a Linux-like OS environment)

If you want to write Chinese in the py file of python2, you must add a line of comments declaring the file encoding, otherwise python2 will use ASCII encoding by default.

# -*- coding:utf-

It should be noted that in addition to the parsing path should be placed on the first line of py, #coding:utf-8, which is generally used to specify the file encoding in Python, should also be specified when needed. , you can put #coding:utf-8 on the second line:

#!/usr/bin/env python
# -*- coding:utf-

If you want to know more about related learning, please pay attention to the php training column!

The above is the detailed content of python why encoding statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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