print dos format file into unix format

Tim Roberts timr at probo.com
Sat Oct 21 22:05:16 EDT 2006


"PengYu.UT at gmail.com" <PengYu.UT at gmail.com> wrote:
>
>Suppose I have a dos format text file. The following python code will
>print ^M at the end. I'm wondering how to print it in unix format.
>
>fh = open(options.filename)
>for line in fh.readlines()
>  print line,

Are you running this on Unix or on DOS?

On Unix, you can do:

for line in open(options.filename).readlines():
    print line.rstrip()

Perhaps quicker is:

sys.stdout.write( open(options.filename).read().replace('\r\n','\n') )
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list