PEP 0263 -- Defining Python Source Code Encodings

单相PWM整流器搭建训练——(一)拓扑原理介绍 单相整流器是一种将交流电转换为直流电的设备,主要由变压器、整流元件和滤波电路组成。变压器将输入的交流电压转换为适合整流的较低电压,整流元件(如二极管)利用其单向导电性将交流电转换为脉动直流电,最后滤波电路去除脉动直流电中的纹波,输出较为平滑的直流电,从而实现交流到直流的转换,广泛应用于各类电源设备中。 阅读详情
PEP: 0263
Title: Defining Python Source Code Encodings
Version: 982904d11574
Last-Modified: 2009-06-04 19:44:37 +0000 (Thu, 04 Jun 2009)
Author: Marc-André Lemburg <mal at lemburg.com>, Martin von Löwis <martin at v.loewis.de>
Status: Final
Type: Standards Track
Created: 06-Jun-2001
Python-Version: 2.3
Post-History:  

Abstract

    This PEP proposes to introduce a syntax to declare the encoding of
    a Python source file. The encoding information is then used by the
    Python parser to interpret the file using the given encoding. Most
    notably this enhances the interpretation of Unicode literals in
    the source code and makes it possible to write Unicode literals
    using e.g. UTF-8 directly in an Unicode aware editor.

Problem

    In Python 2.1, Unicode literals can only be written using the
    Latin-1 based encoding "unicode-escape". This makes the
    programming environment rather unfriendly to Python users who live
    and work in non-Latin-1 locales such as many of the Asian 
    countries. Programmers can write their 8-bit strings using the
    favorite encoding, but are bound to the "unicode-escape" encoding
    for Unicode literals.

Proposed Solution

    I propose to make the Python source code encoding both visible and
    changeable on a per-source file basis by using a special comment
    at the top of the file to declare the encoding.

    To make Python aware of this encoding declaration a number of
    concept changes are necessary with respect to the handling of
    Python source code data.

Defining the Encoding

    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=<encoding name>

    or (using formats recognized by popular editors)

          #!/usr/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 is then interpreted as encoding name. If the encoding
    is unknown to Python, an error is raised during compilation. There
    must not be any Python statement on the line that contains the
    encoding declaration.

    To aid with platforms such as Windows, which add Unicode BOM marks
    to the beginning of Unicode files, the UTF-8 signature
    '\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
    (even if no magic encoding comment is given).

    If a 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:

    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
          ...

Concepts

    The PEP is based on the following concepts which would have to be
    implemented to enable usage of such a magic comment:

    1. The complete Python source file should use a single encoding.
       Embedding of differently encoded data is not allowed and will
       result in a decoding error during compilation of the Python
       source code.

       Any encoding which allows processing the first two lines in the
       way indicated above is allowed as source code encoding, this
       includes ASCII compatible encodings as well as certain
       multi-byte encodings such as Shift_JIS. It does not include
       encodings which use two or more bytes for all characters like
       e.g. UTF-16. The reason for this is to keep the encoding
       detection algorithm in the tokenizer simple.

    2. Handling of escape sequences should continue to work as it does 
       now, but with all possible source code encodings, that is
       standard string literals (both 8-bit and Unicode) are subject to 
       escape sequence expansion while raw string literals only expand
       a very small subset of escape sequences.

    3. Python's tokenizer/compiler combo will need to be updated to
       work as follows:

       1. read the file

       2. decode it into Unicode assuming a fixed per-file encoding

       3. convert it into a UTF-8 byte string

       4. tokenize the UTF-8 content

       5. compile it, creating Unicode objects from the given Unicode data
          and creating string objects from the Unicode literal data
          by first reencoding the UTF-8 data into 8-bit string data
          using the given file encoding

       Note that Python identifiers are restricted to the ASCII
       subset of the encoding, and thus need no further conversion
       after step 4.

Implementation

    For backwards-compatibility with existing code which currently
    uses non-ASCII in string literals without declaring an encoding,
    the implementation will be introduced in two phases:

    1. Allow non-ASCII in string literals and comments, by internally
       treating a missing encoding declaration as a declaration of
       "iso-8859-1". This will cause arbitrary byte strings to
       correctly round-trip between step 2 and step 5 of the
       processing, and provide compatibility with Python 2.2 for
       Unicode literals that contain non-ASCII bytes.

       A warning will be issued if non-ASCII bytes are found in the
       input, once per improperly encoded input file.

    2. Remove the warning, and change the default encoding to "ascii".

    The builtin compile() API will be enhanced to accept Unicode as
    input. 8-bit string input is subject to the standard procedure for
    encoding detection as described above.

    If a Unicode string with a coding declaration is passed to compile(),
    a SyntaxError will be raised.

    SUZUKI Hisao is working on a patch; see [2] for details. A patch
    implementing only phase 1 is available at [1].

Phases

    Implementation of steps 1 and 2 above were completed in 2.3,
    except for changing the default encoding to "ascii".

    The default encoding was set to "ascii" in version 2.5.
   

Scope

    This PEP intends to provide an upgrade path from the current
    (more-or-less) undefined source code encoding situation to a more
    robust and portable definition.

References

    [1] Phase 1 implementation:
        http://python.org/sf/526840
    [2] Phase 2 implementation:
        http://python.org/sf/534304

History

    1.10 and above: see CVS history
    1.8: Added '.' to the coding RE.
    1.7: Added warnings to phase 1 implementation. Replaced the
         Latin-1 default encoding with the interpreter's default
         encoding. Added tweaks to compile().
    1.4 - 1.6: Minor tweaks
    1.3: Worked in comments by Martin v. Loewis: 
         UTF-8 BOM mark detection, Emacs style magic comment,
         two phase approach to the implementation

Copyright

    This document has been placed in the public domain.


5分钟搞懂异构图注意力网络(HAN):从元路径到节点聚合的全流程解析 本文深入解析异构图注意力网络(HAN)的核心原理与实战应用,从元路径设计到多级注意力实现的全流程。HAN通过节点级和语义级双重注意力机制,有效处理包含多种节点和边类型的异构图数据,在电商推荐、学术网络分析等场景展现卓越性能。文章提供DGL实现代码和调参技巧,帮助开发者快速掌握这一强大工具。 阅读详情

相关推荐

黏菌算法(Slime Mould Algorithm,SMA)

这是一篇关于黏菌算法的总结博客,包含算法思想,算法步骤,求函数最值(Python实现),算法改进等,持续更新ing

weixin_46838605的博客 1万+

Python中文报pep-0263错误

其实就是文本字符集不对。 一开始我是: #!/usr/bin/python # -*- coding :UTF-8 -*- print '你好' 老是报错,结果发现就是utf-8 必须要小写。 # -*- coding : utf-8 -*- 或者 # coding=utf-8 #coding:utf-8 #set fileencoding=utf-8 #set fileencoding:u...

misaka10024的博客 2169

PCL 基于法向距离的对应点获取

提出了一种基于法向量欧氏距离的点云配准方法。详细推导了法向量距离计算公式,并给出具体计算流程。在代码实现部分,采用PCL库完成了点云法向量计算和对应关系估计,通过可视化验证了算法有效性。实验结果表明,该方法能够准确获取点云间的对应关系,为后续点云配准奠定了基础。

点云侠的博客 214

Defining Python Source Code Encodings

PEP: 0263 Title: Defining Python Source Code Encodings Version: 982904d11574 Last-Modified: 2009-06-04 19:44:37 +0000 (Thu, 04 Jun 2009) Author: Marc-André Lemburg , Martin vo

luyafei_89430的专栏 1015

python申明utf 8_[Python]编码声明:是coding:utf-8还是coding=utf-8呢

PEP 263 -- Defining Python Source Code Encodings | Python.org https://www.python.org/dev/peps/pep-0263/[Python]编码声明:是coding:utf-8还是coding=utf-8呢_Python_orangleliu 笔记本-CSDN博客 https://blog.csdn.net/oran...

weixin_39934640的博客 456

Python编程问题——中文字符无法被识别,SyntaxError: Non-ASCII character ‘\xe5‘ in file 20.valid_parentheses.py on lin

1. 问题描述 在运行Python代码时出现以下问题: SyntaxError: Non-ASCII character ‘\xe5’ in file 20.valid_parentheses.py on line 29, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 2. 问题产生原因 这个问题产生的原因:通常是Python2的编译器无法识别中文字符。 在Python2中,文字的默认编码方式是A

buzhidao2333shuosha的博客 4711

Scrapy爬取中文数据的问题

Scrapy爬取到的中文数据默认是Unicode编码,显示出来就是这样子:解决方案:在setting.py中,写入:FEED_EXPORT_ENCODING = 'utf-8'即可

KysonLai的博客 1333

PEP 0263 Defining Python Source Code Encodings

 PEP 0263 Defining Python Source Code Encodings Python will default to ASCII as standard encoding if no other encoding hints are given. To define a source

uniqueren的专栏 955

PEP 263 - PEP 0263 -- Defining Python Source Code Encodings

PEP 263 - PEP 0263 -- Defining Python Source Code Encodings PEP: 0263 Title: Defining Python Source Code Encodings Author: Marc-André Lemburg , Martin von Löwis

luoye7422的专栏 953

Python Source Code Encodings

name="中{1}国" print(name) print(name.format("hah","hehe")) 写了一段代码,一运行就报错 File "F:\opensource\pythonDemo\src\test\Haha.py", line 24 Syntax

黄刚的专栏 5075

PEP 263 -- Defining Python Source Code Encodings(定义Python源代码编码)

官方文档:https://www.python.org/dev/peps/pep-0263/ 概要 这个PEP建议引入一个语法来声明Python源文件的编码Python解析器将使用这个编码信息中给定的编码来解释文件。 最引人注意的是,这增强了源代码中Unicode字符的解释。 问题描述 在Python 2.1中,Unicode字符只能使用基于Latin-1的“unicode-e...

weixin_30715523的博客 761

[转]Defining Python Source Code Encodings

http://www.python.org/dev/peps/pep-0263/Defining the EncodingPython will default to ASCII as standard encoding if no other encoding hints are given. To define a source code encoding, ...

weixin_34266504的博客 98

用中文写python_Python 编写代码的时候用中文注释程序会报错,请问大佬该怎么解决?...

刚巧答了一个类似的问题,定义源代码编码为 utf-8 即可,“Defining Python Source Code Encodings”# -*- coding: utf-8 -*-本人python小白,代码照书上打的,但是任然报错,这是什么问题呀?​www.zhihu.com此表达式的第一组然后解释为编码名称。 如果编码对于 Python 是未知的,编译过程中会出现错误。 在包含编码声明的行上...

weixin_39795284的博客 196

python 编码注释问题

Python 官方教程:Source Code Encoding 1. It is possible to use encodings different than ASCII in Python source files. The best way to do it is to put one more special comment line right after the#! line to...

布谷鸟 520

A composite approach to language/encoding detec...

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34254823的博客 1286

python源文件编码的含义_【原创】Python 源文件编码解读

以下内容源于对 PEP-0263 的翻译和解读,同时给出了一些网上网友的说法。======== 我是分割线 ========PEP 0263 -- Defining Python Source Code Encodings【摘要】给出声明 Python 源文件编码的语法。该编码信息后续会被 Python 解析器用于解析源文件。这种方式增强了对源文件中 Unicode 编码字的处理。【问题】Pyt...

weixin_39663378的博客 263

【原创】Python 源文件编码解读

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34362991的博客 158

python解析pdf中文乱码_python数据分析:PDFminer3k解析pdf为文字遇到:WARING:root:GBK-EUC-H-Go语言中文社区...

然后运行自己真正需要的PDF时,报错:刚开始我天真的以为是pdf加密了,后来查了下发现pdfminer3k自带能解密一些简单的加密方法,且遇到加密报错不是这样的。然后重新仔细研究报错,觉得应该是pdf的字体的问题,pdfminer3k不能解析特殊字体,需要下载相应的字体包来解决。字体包下载网站:https://github.com/euske/pdfminer/pull/71/commits/21...

weixin_39783857的博客 1012

Python文件编码---gbk?OR utf8?

Python文件编码---gbk?OR utf8? windows文件名的编码是cp936的,你在使用中文文件名的时候转下码就行了。 比如你python文件编码是utf8 # -*- coding: utf-8 -*- he='开心.mp3' f=open(he.decode('utf-8').encode('cp936'),'w') f.close() ------------

jiangxinyu的专栏 9917

认识Python(二)

第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python3 交互式 —— ipython 集成开发环境 —— PyCharm 第一个 HelloPython 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 文件扩展名 通常都是 .py

小栗子的博客 554

PCode.zip_Polar Coding;MATLAB_pdecode_极化码_极化码 matlab_极化码MATLAB

里面含有极化码的MATLAB仿真程序(源代码),注释也很详细。

20行代码控制42步进电机梯形加减速运动(电机例程分享 第十七期 ).zip

arduino编程控制步进电机实现梯形加减速例程分享

上一篇: 设置python的默认编码为utf8
下一篇: 逆序显示字符串函数
dfdssddfdf
博客等级 码龄15年 9粉丝 12原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值