473,395 Members | 1,456 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,395 software developers and data experts.

stripping

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
<---newline
I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
#if not re.findall(r'^$',line):
print line

but it always give me first 2 lines, ie
abcdefgh
ijklmn

What can i do to make it print all..?
thanks

May 2 '06 #1
2 1191
mi*******@hotmail.com wrote:
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
#if not re.findall(r'^$',line):
print line


you want continue, not break there. but that gives you an infinite loop, so
you need a new exit condition. you're better off scrapping the while and
using a for loop or list comprehension on readline.

May 2 '06 #2
mi*******@hotmail.com wrote:
hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
<---newline
I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
#if not re.findall(r'^$',line):
print line

but it always give me first 2 lines, ie
abcdefgh
ijklmn

What can i do to make it print all..?
thanks

Change the 'break' statement to a 'continue'.

--
Jesse Hager
email = "wr********@tznvy.pbz".decode("rot13")
May 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore...
3
by: Steveo | last post by:
I am currently stripping HTML from a string with the following code. (I know it's not the best way to strip HTML but bear with me) re.compile("<.*?>") I wanted to allow all H1 and H2 tags so i...
4
by: joram gemma | last post by:
Hello, on windows python 2.4.1 I have the following problem >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona' >>> print s D:\music\D\Daniel Lanois\For the beauty of Wynona >>>...
0
by: scott.watson | last post by:
Hello, I am attempting to attach a .pdf file to an email generated using oracle's utl_smtp. I am working on Oracle9i Enterprise Edition Release 9.2.0.4.0. I beleive that the...
1
by: Andy Jefferies | last post by:
I'm having problems stripping out the whitespace at the beginning of a particular element. In the XML snippet I've highlighted tabs and returns as ^I and ^M respectively: <para> ^I ^I ...
2
by: Patrick | last post by:
Hello, after learning that I was taking a class in VB.NET, I have been drafted to solve all my companies VB/scripting problems - hey, I should know everything; I've already taken 6 classes ;) I...
258
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
4
by: Lu | last post by:
Hi, i am currently working on ASP.Net v1.0 and is encountering the following problem. In javascript, I'm passing in: "somepage.aspx?QSParameter=<RowID>Chèques</RowID>" as part of the query...
4
by: Spondishy | last post by:
Hi, I'm looking for help with a regular expression and c#. I want to remove all tags from a piece of html except the following. <a> <b> <h1> <h2>
7
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.