[Python] Coding statement: coding: UTF-8 or coding = urf-8?

Source: Internet
Author: User

We know that the encoding method should be declared in the header file of Python source code. If you don't just use ASCII code, many people may write a little different:

# Coding = UTF-8

# Coding: UTF-8

#-*-Coding: UTF-8 -*-

So how can we effectively write data? What advantages are ineffective?

For more information, see http://www.python.org/dev/peps/pep-0263 /.

A rough look:

Summary:

The purpose of this pep is to introduce how to declare the encoding syntax in a Python source file. The Python interpreter then uses the encoding information when interpreting the file. Most notably, the Unicode interpretation in the source file makes it possible to use FUT-8-like encoding in an editor that recognizes Unicode

How to declare it?

If we do not declare other encoding methods in Python, we use ASCII encoding as the standard encoding method.
To define the encoding method of the source file, a magic declaration should be placed in the first or second line of the file, for example:

 
# Coding = <encoding name>

Or (use the formatting method in the popular editor)

 
#! /Usr/bin/Python #-*-coding: <encoding name> -*-

Or

#! /Usr/bin/Python # VIM: Set fileencoding = <encoding name>:

In either case, the statements on the first or second line must comply with the regular expression.

 
"Coding [: =] \ s * ([-\ W.] + )"

So we can know why we can use the colon or equal sign. If the declared encoding Python cannot be identified, an error is returned.

Examples

These are some examples to clarify the different styles
Defining the source code encoding at the top of a Python source
File:

1. With interpreter binary and using Emacs style file encoding
Comment:

#! /Usr/bin/Python
#-*-Coding: Latin-1 -*-
Import OS, sys
...

#! /Usr/bin/Python
#-*-Coding: iso-8859-15 -*-
Import OS, sys
...

#! /Usr/bin/Python
#-*-Coding: ASCII -*-
Import OS, sys
...

2. Without interpreter line, using plain text:

# This Python file uses the following encoding: UTF-8
Import OS, sys
...

3. Text editors might have different ways of defining the file's
Encoding, e.g.

#! /Usr/local/bin/Python
# Coding: Latin-1
Import OS, sys
...

4. Without encoding comment, Python's parser will assume ASCII
Text:

#! /Usr/local/bin/Python
Import OS, sys
...

5. Encoding comments which don't work:

Missing "coding:" Prefix:

#! /Usr/local/bin/Python
# Latin-1
Import OS, sys
...

Encoding comment not on Line 1 or 2:

#! /Usr/local/bin/Python
#
#-*-Coding: Latin-1 -*-
Import OS, sys
...

Unsupported encoding:

#! /Usr/local/bin/Python
#-*-Coding: utf-42 -*-
Import OS, sys
...

The examples above fully illustrate which statements are correct and which statements are correct.

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.