"Reprint" about the first two lines of the Python script: #!/usr/bin/python and #-*-Coding:utf-8-*-– Specifying the file encoding type

Source: Internet
Author: User

The following content from: Http://www.cnblogs.com/blueskylcc/p/5332642.html, the other side is also rotating, but did not find the source of the reprint;

1, #!/usr/bin/python

is used to illustrate that the scripting language is Python.

is to use the/usr/bin Program (tool) Python, the interpreter, to interpret the Python script to run the Python script.

2, #-*-Coding:utf-8-*-

is used to specify that the file is encoded as Utf-8.

For details, refer to: PEP 0263-defining Python Source Code Encodings

Here, the detailed (mainly translation) explains why this code declaration is added, and how to add the encoding declaration:

2.1 Using file encoding to declare previously encountered problems

Python 2.1, want to enter Unicode characters, only with the Latin-1-based "Unicode-escape" way to enter and for other non-Latin-1 countries and users, want to enter Unicode characters, it seems cumbersome, inconvenient.

Hope is:

Programmers, according to their own preferences and needs, the arbitrary encoding of the input string, can be, this is normal.

2.2 Suggested options for use

That's why THE PEP 0263 is only given to Python's official advice.

This recommendation is:

Allows in a python file, by the beginning of the file, placed in the comments, in the form of a string, declaration of its own python file, with what encoding.

Thus, it is necessary to make a lot of changes, especially the Python file parser, which can recognize this kind of file encoding declaration.

2.3 Specifically how to declare Python file encoding?

The above has been said, is, the beginning of the file, put in the comments, in the form of a statement.

How exactly is it declared, in what format to declare it?

In fact, that's what you've seen before, this:

#-*-coding:utf-8 -*-

A detailed explanation of this format is:

    1. If there is no declaration of this file encoding type, Python is processed by default in ASCII encoding, and if you do not declare the encoding, but the file contains non-ASCII encoded characters, the Python parser to parse the Python file, will naturally error.
    2. Must be placed on the first or second line of a Python file
    3. The supported formats can be three kinds:
      1. With the equals sign:
        # coding=<encoding Name>
      2. Most commonly, with colons (most editors can recognize them correctly):
        #!/usr/bin/python#-*-Coding: <encoding name>-*-
      3. Vim's:
        #!/usr/bin/python# vim:set fileencoding=<encoding name>:
    4. A more precise explanation is:
      • Conforms to the regular expression:
        "^[\t\v]*#.*?coding[:=][\t]* ([-_.a-za-z0-9]+)"
      • , it is clear that if you are familiar with regular expressions, you can write out some other valid code statements, such as Utf-8, for example:
        Coding:         utf-8coding=utf-8coding=                  Utf-8encoding:utf-8crifanencoding=utf-8
    5. In order to take care of special windows with BOM (' \XEF\XBB\XBF ') in UTF-8:

If your Python file itself is encoded as a UTF-8 with a BOM, that is, the first three bytes of the file are: ' \XEF\XBB\XBF ', then:

    • Even if you do not declare the file encoding, also automatically as a UTF-8 encoding
    • If you declare the file encoding, it must be declared (and your file encoding itself consistent) UTF-8, otherwise (due to the declaration of the Code and actual coding inconsistent, natural) will be error;

2.4 Python file encoding declaration follows the philosophy

1. A single full Python source file, with only a single encoding.

Not allowed to embed a variety of encoded data, otherwise it will cause (Python interpreter to parse your Python file) to report encoding errors.

The 2.Python word breaker + compiler will work according to the following logic:

    1. Read file
    2. Different files, resolved to Unicode according to their declared encoding
    3. Convert to UTF-8 string
    4. For UTF-8 strings, go to participle
    5. Compile to create Unicode objects

It is important to note that:

The identifiers in Python are all ASCII.

Write very clearly AH;

"Reprint" about the first two lines of the Python script: #!/usr/bin/python and #-*-Coding:utf-8-*-– Specifying the file encoding type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.