Read text files with a specific encoding? using fileinput?

Jim Correia noone at nowhere.com.invalid
Fri Feb 14 12:47:25 EST 2003


In article <7h34r76sslf.fsf at pc150.maths.bris.ac.uk>,
 Michael Hudson <mwh at python.net> wrote:

> Jim Correia <noone at nowhere.com.invalid> writes:
> 
> > How can I read a test file off disk and interpret it as using a specific 
> > encoding? (Such as utf-8, iso-latin-1, etc.)
> 
> help(codecs.open), I think.

#!/usr/bin/python

import codecs
import sys

f = codecs.open(sys.argv[1], 'r', 'utf8')

for l in f.readlines():
   print "%3s" % len(l)

> > If I have a script which processes files on the command line, using 
> > fileinput, can I do the same?
> 
> Not sure about that one.

Unless I'm missing something (which is obvious - I'm a python newbie) it 
isn't possible (that is unless there is a way to change the default 
encoding for reading files - is that possible?)

I got fileinput with my standard python install. What is the process for 
suggesting an enhancement or submitting a patch?

Thanks,
Jim




More information about the Python-list mailing list