My script first reads a data file and then proceeds to analyze/plot it. I want the program to read the file line by line and then stop looking for lines upon hitting a # in the data file, at which point I'd like it to continue on the the rest it's code (plotting/analyzing, etc). I've been looking around and have tried sys.exit()
and break
but they don't allow for the program to continue on to it's analytic process, it just exits. Totally lost.
For more info....
for i in range(WL):
if(data[i][0]=='#'):comments=comments+1
else:
#analytic stuff
#more stuff
That was what was originally written in. This isn't my stuff, so I'm finding I get errors with this format, I'd like to revamp it and make the # a termination character. After trying @Lev_Levitsky's suggestion I now get an error of the type
flux=np.append(flux,data[i][1])
IndexError: list index out of range
Why would that happen?
Here's the new look
for i in range(WL):
if '#' in line:
break
else:
#analyze
break
is what you need. It will take you out of the loop where you read lines from the file, and take you to whatever is after the loop:
with open('data') as f:
for line in f:
if '#' in line:
break
# do something else in the loop
# like append something to a list
# analyze the data
Based on what you just showed us, what you want to do is this:
for i in range(WL):
if(data[i][0]=='#'):
comments=comments+1
break
else:
#analytic stuff
#more stuff
This is exactly the same as what Lev suggested, except I've spelled out explicitly where to put your break.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.