Read text files with a specific encoding? using fileinput?

Jim Correia noone at nowhere.com.invalid
Fri Feb 14 21:30:37 CET 2003


In article <mailman.1045245586.481.python-list at python.org>,
 Skip Montanaro <skip at pobox.com> wrote:

>     >> > If I have a script which processes files on the command line, using 
>     >> > fileinput, can I do the same?
>     >> 
>     >> Not sure about that one.
> 
>     Jim> Unless I'm missing something (which is obvious - I'm a python
>     Jim> newbie) it isn't possible (that is unless there is a way to change
>     Jim> the default encoding for reading files - is that possible?)
> 
> Assuming the file was already opened using the proper mode, how about this
> slight (and untested) simplification of codecs.open?
> 
>     def encode_file(f, encoding=None, errors='strict'):
>         if encoding is None:
>             return f
>         (e, d, sr, sw) = lookup(encoding)
>         srw = StreamReaderWriter(f, sr, sw, errors)
>         # Add attributes to simplify introspection
>         srw.encoding = encoding
>         return srw

I could write my own replacement for fileinput that did that, but 
currently the fileinput module does all the opening, so absent an open 
hook or a way to tell it what encoding to use, I'm not sure that will 
help.

Am I missing something?

Jim




More information about the Python-list mailing list