Start New Discussion within our Software Development Community

I'm quite new in Python.
I've been trying to solve this IndexError but couldn't get the answer yet.
Need you guys advice or help.

I want to extract the sentence matched with the keyword strSearch from 5gm-007 file(just text file).
The error occured in "str5gram = lines[a]".
here is the code.

import nltk
import string
import sys
tx = open(r"d:\##DB\Google-5gram\5gm-0007")
a           = 0
i           = 0
strSearch   = "all carried out"
nRange      = 0
weight      = range(20)

while nRange<600:
    a = 0
    lines = tx.readlines(100)
    for x in range(296):
        str5gram = lines[a]
        if strSearch in str5gram:
            strSplit5gram = str5gram.split()
            weight[i] = strSplit5gram[5]
            i = i + 1
            print "rommend word is", strSplit5gram[4], weight
            print a,"th line"
        a = a+1
    nRange = nRange + 295

and this is the result and error system displaying.

rommend word is at
231 th line
rommend word is by
232 th line
rommend word is in
233 th line
rommend word is on
234 th line
rommend word is to
235 th line
rommend word is under
236 th line
rommend word is with
237 th line
rommend word is without
238 th line

Traceback (most recent call last):
File "<string>", line 97, in run
File "C:\Python25\Lib\", line 366, in run
exec cmd in globals, locals
File "C:\Users\Sky\Desktop\Python Study\", line 19, in <module>
str5gram = lines[a]
IndexError: list index out of range

Probably beacuse uou only have 239 lines, and are cycling the variable that use as index to a higher number then the elements of the list.

You should count the elements and cycle between that counting.

Cheers and Happy coding

This article has been dead for over six months. Start a new discussion instead.