By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,446 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,446 IT Pros & Developers. It's quick & easy.

print dos format file into unix format

P: n/a
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,

Thanks,
Peng

Oct 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Pe*******@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,

Thanks,
Peng
open(outfile, "wb").write(open(infile, "rb").read().replace("\r", ""))

Or something like that ... :)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 21 '06 #2

P: n/a
"Pe*******@gmail.com" <Pe*******@gmail.comwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 22 '06 #3

P: n/a
Pe*******@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,

Thanks,
Peng
Python ships with two utility scripts, crlf.py and lfcr.py, that
"Replace CRLF with LF in argument files" and "Replace LF with CRLF in
argument files", respectively.

Look in examples/Tools/scripts of your python dist.

Even if you don't want to use the scripts, you can read them for
insight.

Peace,
~Simon

Oct 22 '06 #4

P: n/a
Tim Roberts wrote:
"Pe*******@gmail.com" <Pe*******@gmail.comwrote:
>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') )
There are more differences between text files than that.
I don't know any unix systems that uses CP 437 etc. I'd
convert the text to unicode through .decode('cp437') etc,
and then print that. If things aren't set up so than
unicode object print correctly, use
..decode('cp437').encode('utf8') etc to get it to an
appropriate encoding.
Oct 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.