MikG
MikG

Reputation: 1019

Python, iterating through a list to perform a search

I hope someone can point out where I have gone wrong. I am looking to iterate through the 'mylist' list to grab the first entry and use that first entry as a search string, then perform a search and gather particular information once the string is found and post it to an Excel worksheet. Then I am hoping to iterate to the next 'mylist' entry and perform another search. The first iteration performs ok, but with the second iteration of the loop I get the following CMD window error...

2014 Apr 25  09:43:42.080  INFORMATION FOR A

14.01
Traceback (most recent call last):
File "C:\TEST.py", line 362, in <module>
duta()
File "C:\TEST.py", line 128, in duta
if split[10] == 'A':
IndexError: list index out of range
Exception RuntimeError: RuntimeError('sys.meta_path must be a list of 
import hooks',) in <bound method Workbook.__del__ of 
<xlsxwriter.workbook.Workbook object at 0x0238C310>> ignored

Here's my 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()

                    row_numBMAA+=1 

                    num = 1
                    b = 1
                    print len(mylist)
                    print (mylist[num])
                    while b<len(mylist):
                        for i, line in enumerate(searchlinesBMA):
                            for word in [mylist[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[6] == 'A':
                                        print keylineBMA
                                        print valueR[3]
                                        worksheetFILTERA.write(row_numBMAA,3,valueR[3], decimal_format)

                                        row_numBMAA+=1
                                        break

                        num+=1
                        b=+1

Any ideas as to what I am doing wrong? Is my loop out of position, or am I not inputting the correct list pointer?

Thanks, MikG

Upvotes: 0

Views: 2078

Answers (2)

Tim D
Tim D

Reputation: 1743

In my experience, this error is related to garbage collecting out of order. I saw it once when I was debugging code where someone was writing to files in a __del__ method. (Bad idea). I'm pretty sure you're getting the error because you're closing the file inside a with: block, which does the open and close for you.

Upvotes: 3

Mattias Backman
Mattias Backman

Reputation: 957

On the second run, you got split = keylineBMA.split() with a result shorter than you expected. You try to access index 10 which is outside the list.

Upvotes: 1

Related Questions