Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

On the definition of python Source character Encoding

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Run the following Python print statement:

Print I "said" do not touch "this."

It contains a double quotation mark in Chinese and the python interpreter reports an error. The error message is as follows:

[wangy@bogon document] $python ex1.py

File "ex1.py", line 7

SyntaxError: Non-ASCII character'\ xe2' in file ex1.py on line 7, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

View the link http://www.python.org/peps/pep-0263.html

The main contents are as follows:

In the Python2.1 version, the source file only supports Latin-1, the character encoding of Western European countries, which causes a lot of trouble to Asian programmers, so it is necessary to use "unicode-escape" coding to represent Unicode literals.

The solution is that in order for the interpreter to know the encoding of the source code, the encoding of the source file must be declared.

Define how to encode:

Python will default to ASCII as standard encoding if no other encoding hints are given.

To define a source code encoding, a magic comment must be placed into the source

Files either as first or second line in the file, such as:

# coding=

Or (using formats recognized by popular editors):

#! / usr/bin/python

#-*-coding:-*

Or:

#! / usr/bin/python

# vim: set fileencoding=:

It is best to use the first or the second.

In particular, it is mentioned that under the windows platform, the Unicode BOM tag is added to the Unicode file header, so there is no need to declare the file encoding. Similarly, the UTF-8 tag will be added to the UTF-8 file header, so there is no need to declare.

If the source file uses both the UTF-8 BOM mark signature and a magic encoding comment, the only allowed encoding for the comment is' utf-8'. Any other encoding will cause an

Error.

Examples

These are some examples to clarify the different styles for defining the source code encoding at the top of a Python source file:

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

...

Without interpreter line, using plain text:

# This Python file uses the following encoding: utf-8

Import os, sys

...

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

#! / usr/local/bin/python

# coding: latin-1

Import os, sys

...

Without encoding comment, Python's parser will assume ASCII text:

#! / usr/local/bin/python

Import os, sys

...

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

...

Modify the source code and save it in UTF-8. The editor uses gedit under Linux.

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

Print "hello world!"

Print "hello Again"

Print "I like trying this"

Print "This is fun"

Print 'Yay! Printing'

Print "I'd much rather you 'not'."

Print upright I "said" there are Chinese double quotes "this."

Normal printing

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report