V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shalk
V2EX  ›  Python

关于 python 源码编码的小疑惑?

  •  
  •   shalk · 2016-09-18 10:46:36 +08:00 · 2831 次点击
    这是一个创建于 2776 天前的主题,其中的信息可能已经有所发展或是发生改变。

    按照 https://www.python.org/dev/peps/pep-0263/ 给出了编码定义,格式应该如下

    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> 
    

    我有几个疑问

    1. # coding=utf-8# coding=UTF-8 有什么区别

    2. 源码使用 utf-8 情况下,应该使用 without BOM 么,为什么?

    3. 这种是错误的么,生效么

      # -*- encoding:  UTF-8 -*-
      
    15 条回复    2016-09-18 18:32:55 +08:00
    wellsc
        1
    wellsc  
       2016-09-18 10:51:57 +08:00 via Android
    You are so 闲。
    julyclyde
        2
    julyclyde  
       2016-09-18 14:27:49 +08:00
    # 没区别
    # 因为 BOM 影响 hashbang 的判读
    # 看起来是正确的啊
    shalk
        3
    shalk  
    OP
       2016-09-18 15:44:09 +08:00
    @julyclyde encoding 和 coding 看上去不一样啊。 PEP 中也没提到有 encoding:这种写法
    zhuangzhuang1988
        4
    zhuangzhuang1988  
       2016-09-18 16:25:46 +08:00
    看 python 源码就知道了..
    bombless
        5
    bombless  
       2016-09-18 16:37:01 +08:00   ❤️ 1
    参考了下 https://github.com/python-git/python/blob/715a6e5035bb21ac49382772076ec4c630d6e960/Lib/idlelib/IOBinding.py
    1 肯定是没区别, 2 的话代码里面有说, 2.3 才开始支持, 3 的话可以,代码里面用到
    glasslion
        6
    glasslion  
       2016-09-18 16:47:35 +08:00   ❤️ 1
    @shalk
    实测 1 , 3 里的写法都不会报错, 但这和 CPython 的实现有关, 不应该使用 PEP 263 里没提到的格式

    2. 应该使用不带 BOM 的。 BOM 本来就是针对 utf-16 实现一个十分丑陋的方案。 utf-8 里使用 BOM 没有任何意义。
    Kisesy
        7
    Kisesy  
       2016-09-18 17:29:56 +08:00   ❤️ 1
    utf-8 和 UTF-8 没区别,甚至写成 utf8 和 u8 都行
    julyclyde
        8
    julyclyde  
       2016-09-18 17:30:37 +08:00
    @shalk 看上去确实不一样。不过你到底仔细读了 PEP 了没?
    fugees
        9
    fugees  
       2016-09-18 17:51:27 +08:00
    蛋痛的厉害吧
    我一直用 # encoding: utf-8
    jiangbingo
        10
    jiangbingo  
       2016-09-18 17:59:25 +08:00
    对了 ,写脚本文件一定要将 -*- encoding:utf-8-*- 写在代码开始的第一行。不然会报编码错~
    tmackan
        11
    tmackan  
       2016-09-18 18:22:02 +08:00
    @wellsc 哈哈哈
    shalk
        12
    shalk  
    OP
       2016-09-18 18:28:21 +08:00
    @bombless 感谢分享

    另外,我在源码中没有找到关于大小写 区分的 解释;

    我看到 `def coding_spec` 解析源码的开头两行,返回编码的 name ;之后用 return unicode(chars, enc) 解码成 unicode 。

    我自己试了一下。

    ```
    >>> a="啊啊"
    >>> unicode(a,"utf-8")
    u'\u554a\u554a'
    >>> unicode(a,"UTF-8")
    u'\u554a\u554a'
    ```

    另外又看了一下 https://docs.python.org/2/library/codecs.html#standard-encodings

    也没有找到关于大小写的解释..
    tmackan
        13
    tmackan  
       2016-09-18 18:30:26 +08:00
    @shalk 哥们,你扣的太细了吧 ==
    shalk
        14
    shalk  
    OP
       2016-09-18 18:31:09 +08:00
    @Kisesy 果然是这样。

    还是可以是 utf ,或者下面这几个。
    ```
    # utf_8 codec
    'u8' : 'utf_8',
    'utf' : 'utf_8',
    'utf8' : 'utf_8',
    'utf8_ucs2' : 'utf_8',
    'utf8_ucs4' : 'utf_8',
    ```
    shalk
        15
    shalk  
    OP
       2016-09-18 18:32:55 +08:00
    @tmackan 也想学习 python 的方方面面。 ^_^
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1337 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:35 · PVG 07:35 · LAX 16:35 · JFK 19:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.