8000 Detect pep-0263 · Issue #249 · chardet/chardet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Detect pep-0263 #249
Open
Open
@patrikha

Description

@patrikha

It would be great if the following header is supported:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

please see:
pep-0263

hax workaround:

def get_encoding(path):
    if not isinstance(path, Path):
        path = Path(path)
    with open(path, 'rb') as stream:
        raw_bytes = stream.read(1024)
    encoding = chardet.detect(raw_bytes)['encoding']
    if path.suffix.lower() == '.py':
        start_pos = raw_bytes.find(b'-*- coding: ')
        if start_pos != -1:
            start_pos += 12
            end_pos = raw_bytes[start_pos:].find(b' -*-')
            if end_pos:
                end_pos += start_pos
                coding = raw_bytes[start_pos:end_pos]
                encoding = coding.decode(encoding)
    return encoding
< 3D09 /div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0