473,324 Members | 2,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,324 software developers and data experts.

'While' question

Hi -

I am writing now my second script ever in python and need some help with
'while'. I am reading text from a set of files and manipulating the data
somehow. I use 'while 'word' not in line' to recognize words in the
texts. Sometimes, the files are empty, so while doesn't find 'word' and
runs forever. I have two questions:
1) how do I overcome this, and make the script skip the empty files?
(should I use another command?)
2) how do I interrupt the code without closing Python? (I have ActivePython)

I do know that the strings I am searching for are within the first say
50 lines.

Thanks!

Code:
|while 'PRIMARY' not in line:
line = f.readline()[:-1]
# copy scores
while 'es' not in line:
line = f.readline()[:-1]
out_file.write(line)
out_file.write(' ')
print
out_file.write('\n')
f.close()
out_file.close()

For example, 'PRIMARY' and 'es' do not exist when the file I am reading
(f) is empty.
|
Aug 21 '08 #1
6 1103
On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
somehow. I use 'while 'word' not in line' to recognize words in the
texts. Sometimes, the files are empty, so while doesn't find 'word' and
runs forever. I have two questions:
1) how do I overcome this, and make the script skip the empty files?
(should I use another command?)
2) how do I interrupt the code without closing Python? (I have ActivePython)
Try the docs first. You need to read about 'continue' and
'break' statements: http://docs.python.org/tut/node6.html

HTH.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Aug 21 '08 #2
Thanks for the reference. I tried it with a general example and got it
to work - I used an index that counts up to a threshold that is set to
break. It does not work though with my real code. I suspect this is
because I cannot really read any lines from an empty file, so the code
gets stuck even before I get to j=j+1:

line = f.readline()[:-1]
j=0
while 'PRIMARY' not in line:
line = f.readline()[:-1]
j=j+1
if j==30:
break

Any suggestions?

BK
Wojtek Walczak wrote:
On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
>somehow. I use 'while 'word' not in line' to recognize words in the
texts. Sometimes, the files are empty, so while doesn't find 'word' and
runs forever. I have two questions:
1) how do I overcome this, and make the script skip the empty files?
(should I use another command?)
2) how do I interrupt the code without closing Python? (I have ActivePython)

Try the docs first. You need to read about 'continue' and
'break' statements: http://docs.python.org/tut/node6.html

HTH.

Aug 21 '08 #3
Ben Keshet wrote:
Thanks for the reference. I tried it with a general example and got it
to work - I used an index that counts up to a threshold that is set to
break. It does not work though with my real code. I suspect this is
because I cannot really read any lines from an empty file, so the code
gets stuck even before I get to j=j+1:

line = f.readline()[:-1]
j=0
while 'PRIMARY' not in line:
line = f.readline()[:-1]
j=j+1 if j==30:
break
Any suggestions?

BK
Wojtek Walczak wrote:
>On Thu, 21 Aug 2008 18:01:25 -0400, Ben Keshet wrote:
>>somehow. I use 'while 'word' not in line' to recognize words in the
texts. Sometimes, the files are empty, so while doesn't find 'word'
and runs forever. I have two questions:
1) how do I overcome this, and make the script skip the empty files?
(should I use another command?)
2) how do I interrupt the code without closing Python? (I have
ActivePython)

Try the docs first. You need to read about 'continue' and
'break' statements: http://docs.python.org/tut/node6.html

HTH.

You might consider turning this around into something like:
for j, line in enumerate(f):
if 'PRIMARY' in line:
continue

if j == 30:
break

IMHO this is MUCH easier to understand.

-Larry
Aug 21 '08 #4
On Aug 22, 9:01 am, Ben Keshet <kesh...@umbc.eduwrote:
Thanks for the reference. I tried it with a general example and got it
to work - I used an index that counts up to a threshold that is set to
break. It does not work though with my real code. I suspect this is
because I cannot really read any lines from an empty file, so the code
gets stuck even before I get to j=j+1:

line = f.readline()[:-1]
j=0
while 'PRIMARY' not in line:
line = f.readline()[:-1]
j=j+1
if j==30:
break

Any suggestions?
(1) don't top-post
(2) use a 'for' statement
(3) readline is antique
(4) don't throw away the last character in the line without knowing
what it is

for line in f:
line = line.rstrip('\n')
# do something useful here
if 'PRIMARY' in line:
break
# do more useful stuff here

A quick rule of thumb for Python: if your code looks ugly or strained
or awkward, it's probably also wrong.

HTH,
John
Aug 21 '08 #5
John Machin a écrit :
(snip)
A quick rule of thumb for Python: if your code looks ugly or strained
or awkward, it's probably also wrong.
+1 QOTW
Aug 22 '08 #6
Bruno Desthuilliers <br********************@websiteburo.invalidwrite s:
John Machin a Ă©crit :
(snip)
A quick rule of thumb for Python: if your code looks ugly or
strained or awkward, it's probably also wrong.

+1 QOTW
Merely a special case of the truism that "Your code is probably wrong
(regardless of any other properties it may show)".

That doesn't make John's quote any less worthy of QOTW, though :-)

--
\ “The problem with television is that the people must sit and |
`\ keep their eyes glued on a screen: the average American family |
_o__) hasn't time for it.” —_The New York Times_, 1939 |
Ben Finney
Aug 23 '08 #7

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

Similar topics

1
by: jjliu | last post by:
Could someone point me out that why the following perl program only print 'title'. I expect the contents in h1, keywords and description to print out as well. thanks. JJL #!/usr/bin/Perl ...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
5
by: J. Yuan | last post by:
Hi, I am working on a checkout/inventory system. How can I make a button that when pressed, would update the previous fields transaction number to a table (for example, -3 printers, so that...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
8
by: jvb | last post by:
Hey all, I figure it's Wednesday, why not put a question up for debate. Beyond personal preference, is there any benefit (performance or otherwise) to using one loop over the other? For example,...
9
by: morpheus | last post by:
Hi Group, When I run this: # include <stdio.h> int main(){ int c=0; while ( (c=getchar()) != EOF && c != ' ' && c != '\t' ) printf("foo"); if (c == '8') putchar(c);
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.