Python file Encoding Description coding=utf-8

Source: Internet
Author: User

Python supports 3 kinds of code declarations, commonly used to see the following two

1.# -*-coding:utf-8-*-

This is written to be compatible with Emacs's code declaration.

2. Short, but Emacs cannot be used # coding=utf-8

A little shorter, but Emacs can't be used

The reason to declare an unencrypted type is mainly the problem of Chinese error.

At the beginning of the Python file (typically the first or second line), use the encoding used to describe your Python source program file. By default, your program needs to write in ASCII code, but if you write Chinese in it, the Python interpreter will usually make an error, but if you add the file encoding you use, Python will automatically process the error again.

Here's what to note:

1.coding later use ":" or "=" can

2. However, ":" or "=" must have no spaces between coding. before I tried to have a space declaration failed, or not support Chinese. As for ":" or "=" after, there is no space on the so-called.

See: https://www.python.org/dev/peps/pep-0263/

Defining the Encoding
    Python would default to ASCII as standard encoding if no other    encoding hints is given.    To define a source code encoding, a magic comment must is placed into the    source files either as first or second    l Ine in the file, such as:          # coding=<encoding name>    or (using formats recognized by popular editors)          #!/u Sr/bin/python          #-*-Coding: <encoding name>-*-    or          #!/usr/bin/python          # vim:set fileencoding= <encoding name>: More    precisely, the first or second line must match the regular    expression "coding[:=]\s * ([-\w.] +)". The first group of this    expression was then interpreted as encoding name. If the encoding    is unknown to Python, an error is raised during compilation. There must not being any Python statement on the line that    contains the    encoding declaration.

Python file Encoding Description coding=utf-8

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.