print dos format file into unix format

P

PengYu.UT

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
 
D

Dennis Lee Bieber

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,
Open in "universal line ending" mode, strip the trailing new-line,
and don't use the comma on the print?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Steve Holden

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
 
T

Tim Roberts

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') )
 
S

Simon Forman

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
 
M

Magnus Lycka

Tim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top