473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

print dos format file into unix format

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
4 3477
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: krishnakant Mane | last post by:
hello all. I am developing an ncurses based python application that will require to create pdf reports for printing. I am not using py--qt or wx python. it is a consol based ui application and I...
4
by: foal11 | last post by:
Hi all, My problem is that I have generated a text file on unix which I want to print on a neoware(Neolinux) box. The problem is that the line length is 2 big so when I print it the text is...
2
by: foal11 | last post by:
Hi all, My problem is that I have generated a text file on unix which I want to print on a neoware(Neolinux) box. The problem is that the line length is 2 big so when I print it the text is...
2
by: jims | last post by:
Hi. I was wondering if anybody could piont me to a reference for the .bak file format. I need to upload a huge amount of data from a unix machine into an MS SQLServer database and have tried...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.