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.
| 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/
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.
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
...
|
by: jeff |
last post by:
how do you convert form byte to Int32 while retaining the binary
value of the byte array
|
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...
|
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)
|
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
|
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?
|
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,...
|
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);
|
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...
|
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):
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |