'While' question

B

Ben Keshet

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.
|
 
B

Ben Keshet

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
 
L

Larry Bates

Ben said:
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 said:
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
 
J

John Machin

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
 
B

Bruno Desthuilliers

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
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top