Read text files with a specific encoding? using fileinput?

Jim Correia noone at nowhere.com.invalid
Fri Feb 14 19:55:51 EST 2003


In article <874r76cs39.fsf at cassandra.evansnet>,
 Tim Evans <t.evans at paradise.net.nz> wrote:

> Telling it how to open files can be done:
> 
> -----------------------------------------------
> import fileinput, codecs
> 
> def my_open(filename, mode):
>     return codecs.open(filename, mode, 'utf8')
> 
> fileinput.open = my_open
> 
> for line in fileinput.input():
>     print repr(line)
> -----------------------------------------------
> 
> Making it read utf8 from stdin will be a little harder, but changing
> 'fileinput.sys' to a new object with hand-crafted 'stdin' and 'argv'
> attributes should work.

Thanks for the pointer. I should be able to cobble something together 
with this information.

Jim




More information about the Python-list mailing list