Reputation: 1019
I'll be honest and admit that I asked an earlier related question, but I have since found a solution to the earlier issue and am now hitting another issue! I am iterating through a list to search a large text file. The search involves taking the first list entry and using that string (of digits) it goes through the text file searching for that entry. The script works ok up to the last iteration/last list entry, whereby I get the following error in cmd window...
2014 Apr 25 09:46:58.884 [35] 0x5245 FFFF Rec rst 4444 A
18.84
2014 Apr 25 09:46:58.902 [81] 0x5245 FFFF Rec rst 4444 A
19.62
2014 Apr 25 09:46:58.944 [2B] 0x5245 FFFF Rec rst 4444 A
16.69
Traceback (most recent call last):
File "C:\MEAN_val.py", line 361, in <module>
duta()
File "C:\MEAN_val.py", line 120, in duta
for word in [framelist[num]]:
IndexError: list index out of range
Exception RuntimeError: RuntimeError('sys.meta_path must be a list of import hoo
ks',) in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object
at 0x0241F2F0>> ignored
It seems that the while num<=len(framelist): function isn't working and it is not exiting the loop once the num reaches the length of the list.
Here's my partially working code...
for root, subFolders, files in chain.from_iterable(os.walk(path) for path in paths):
for filename in files:
if filename.endswith('.txt'):
with open(os.path.join(root, filename), 'r') as fBMA:
searchlinesBMA = fBMA.readlines()
fBMA.close()
num = 0
while num<=len(framelist):
for i, line in enumerate(searchlinesBMA):
for word in [framelist[num]]:
if word in line:
keylineBMA = searchlinesBMA[i-2]
Rline = searchlinesBMA[i+10]
Rline = re.sub('[()]', '', Rline)
valueR = Rline.split()
split = keylineBMA.split()
if split[10] == 'A':
print keylineBMA
print valueR[3]
num+=1
break
Thanks for reading, MikG
Upvotes: 0
Views: 186
Reputation: 1019
Decided to cleanly exit the loop using the following code. Written Excel matches up anyway, so still a bit unsure why the Index Error was there?
except IndexError:
pass
continue
Upvotes: 0
Reputation: 31
You can change your code to:
while num<len(framelist)
because the length is simply a count. If you start at zero and go to (including) the length, you are going through the loop (length + 1) times
Upvotes: 2