[Tutor] reading lines from a list of files

Albert-Jan Roskam fomcl at yahoo.com
Wed May 13 07:45:26 CEST 2015


------------------------------
On Tue, May 12, 2015 9:54 PM CEST Peter Otten wrote:

>Albert-Jan Roskam wrote:
>
>> It was not that long ago that I found out about the fileinput module, so I
>> sometimes forget to use it. It is not specify the encoding of the files,
>> is it? It'd be nice if one could specify a tuple of encodings, e.g.
>> ('utf-8-sig', 'latin1', 'cp1252').
>> 
>> input(files=None, inplace=0, backup='', bufsize=0, mode='r',
>> openhook=None)
>>         input([files[, inplace[, backup[, mode[, openhook]]]]])
>
>Whatever you plan to do with these encodings, it should be possible with a custom openhook.

Hi Peter,

Sorry, I should have read the documentation better. I did not know about openhooks. There's one useful hook already:

fileinput.hook_encoded(encoding)
Returns a hook which opens each file with codecs.open(), using the given encoding to read the file.
Usage example: 
fi = fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))
Note With this hook, FileInput might return Unicode strings depending on the specified encoding.
New in version 2.5.



More information about the Tutor mailing list