Note that there are some explanatory texts on larger screens.

plurals
  1. PORead a unicode file in python which declares its encoding in the same way as python source
    primarykey
    data
    text
    <p>I wish to write a python program which reads files containing unicode text. These files are normally encoded with UTF-8, but might not be; if they aren't, the alternate encoding will be explicitly declared at the beginning of the file. More precisely, it will be declared using exactly the same rules as Python itself uses to allow Python source code to have an explicitly declared encoding (as in PEP 0263, see <a href="https://www.python.org/dev/peps/pep-0263/" rel="nofollow noreferrer" title="PEP 0263 -- Defining Python Source Code Encodings">https://www.python.org/dev/peps/pep-0263/</a> for more details). Just to be clear, the files being processed are not actually python source, but they do declare their encodings (when not in UTF-8) using the same rules.</p> <p>If one knows the encoding of a file before one opens it, Python provides a very easy way to read the file with automatic decoding: the <code>codecs.open</code> command; for instance, one might do:</p> <pre><code>import codecs f = codecs.open('unicode.rst', encoding='utf-8') for line in f: print repr(line) </code></pre> <p>and each <code>line</code> we get in the loop will be a unicode string. Is there a Python library which does a similar thing, but choosing the encoding according to the rules above (which are Python 3.0's rules, I think)? (e.g. does Python expose the 'read file with self-declared encoding' it uses to read source to the language?) If not, what's the easiest way to achieve the desired effect?</p> <p>One thought is to open the file using the usual <code>open</code>, read the first two lines, interpret them as UTF-8, look for a coding declaration using the regexp in the PEP, and if one finds one start decoding all subsequent lines using the encoding declared. For this to be sure to work, we need to know that for all the encodings that Python allows in Python source, the usual Python <code>readline</code> will correctly split the file into lines - that is, we need to know that for all the encodings Python allows in Python source, the byte string '\n' always really mean newline, and isn't part of some multi-byte sequence encoding another character. (In fact I also need to worry about '\r\n' as well.) Does anyone know if this is true? The docs were not very specific. </p> <p>Another thought is to look in the Python sources. Does anyone know where in the Python source the source-code-encoding-processing is done?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload