python - 获取 magic line/shebang 中指定的编码(从模块内)

标签 python python-2.7 encoding character-encoding

如果我在“魔法线”或 python 模块的 shebang 中指定字符编码(如 PEP 263 所建议的那样)

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

我可以从那个模块中检索这个编码吗?

(使用 Python 2.7.9 在 Windows 7 x64 上工作)


我尝试(但没有成功)检索默认编码或 shebang

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

import sys
from shebang import shebang

print "sys.getdefaultencoding():", sys.getdefaultencoding()
print "shebang:", shebang( __file__.rstrip("oc"))

将产生:

sys.getdefaultencoding(): ascii

shebang: None

(与 iso-8859-1 相同)

最佳答案

我会借用 Python 3 tokenize.detect_encoding() function在 Python 2 中,稍微调整以符合 Python 2 的期望。我已经更改了函数签名以接受文件名并删除了到目前为止读取的行的包含;你不需要那些用于你的用例:

import re
from codecs import lookup, BOM_UTF8

cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)')
blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)')

def _get_normal_name(orig_enc):
    """Imitates get_normal_name in tokenizer.c."""
    # Only care about the first 12 characters.
    enc = orig_enc[:12].lower().replace("_", "-")
    if enc == "utf-8" or enc.startswith("utf-8-"):
        return "utf-8"
    if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \
       enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):
        return "iso-8859-1"
    return orig_enc

def detect_encoding(filename):
    bom_found = False
    encoding = None
    default = 'ascii'

    def find_cookie(line):
        match = cookie_re.match(line)
        if not match:
            return None
        encoding = _get_normal_name(match.group(1))
        try:
            codec = lookup(encoding)
        except LookupError:
            # This behaviour mimics the Python interpreter
            raise SyntaxError(
                "unknown encoding for {!r}: {}".format(
                    filename, encoding))

        if bom_found:
            if encoding != 'utf-8':
                # This behaviour mimics the Python interpreter
                raise SyntaxError(
                    'encoding problem for {!r}: utf-8'.format(filename))
            encoding += '-sig'
        return encoding

    with open(filename, 'rb') as fileobj:        
        first = next(fileobj, '')
        if first.startswith(BOM_UTF8):
            bom_found = True
            first = first[3:]
            default = 'utf-8-sig'
        if not first:
            return default

        encoding = find_cookie(first)
        if encoding:
            return encoding
        if not blank_re.match(first):
            return default

        second = next(fileobj, '')

    if not second:
        return default    
    return find_cookie(second) or default

和原来的函数一样,上面的函数从源文件中读取两行max,如果cookie中的编码无效或者是当存在 UTF-8 BOM 时不是 UTF-8。

演示:

>>> import tempfile
>>> def test(contents):
...     with tempfile.NamedTemporaryFile() as f:
...         f.write(contents)
...         f.flush()
...         return detect_encoding(f.name)
...
>>> test('# -*- coding: utf-8 -*-\n')
'utf-8'
>>> test('#!/bin/env python\n# -*- coding: latin-1 -*-\n')
'iso-8859-1'
>>> test('import this\n')
'ascii'
>>> import codecs
>>> test(codecs.BOM_UTF8 + 'import this\n')
'utf-8-sig'
>>> test(codecs.BOM_UTF8 + '# encoding: latin-1\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in test
  File "<string>", line 37, in detect_encoding
  File "<string>", line 24, in find_cookie
SyntaxError: encoding problem for '/var/folders/w0/nl1bwj6163j2pvxswf84xcsjh2pc5g/T/tmpxsqH8L': utf-8
>>> test('# encoding: foobarbaz\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in test
  File "<string>", line 37, in detect_encoding
  File "<string>", line 18, in find_cookie
SyntaxError: unknown encoding for '/var/folders/w0/nl1bwj6163j2pvxswf84xcsjh2pc5g/T/tmpHiHdG3': foobarbaz

关于python - 获取 magic line/shebang 中指定的编码(从模块内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38374489/

相关文章:

Python,试图打开一个命令

python - 如何在 pandas + python 中将值从一行传递到下一行,并使用它递归计算相同的后续值

audio - 从Hi8-Lp格式转换视频

python - 有没有办法在 Python 中为 lxml 指定固定(或可变)数量的元素

python - Rabbitmq 发送 ack 时断开连接

python - 如何使用 beautiful soup python 提取 href、alt 和 imgsrc

python - 在 pytest 中参数化并运行单个测试

python - 为什么 "nparray.tolist()"占用这么多空间?

c - 在 C 程序中可能是 "force"UTF-8 吗?

c# - 如何避免参数化 SQL 查询中的无效 UTF8 字节序列?