Read text files with a specific encoding? using fileinput?
Skip Montanaro
skip at pobox.com
Fri Feb 14 12:58:15 EST 2003
>> > 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
Skip
More information about the Python-list
mailing list