0

I'm getting the IndexError: string index out of range. Each line in the file "document_words" ends with "-99". So i think error may be because "-99" is not converted to int. But i'm not sure. If that is the case how can i convert "-99" to int and break from the loop.

Following is my code:

words=open('words','r')
image=open('document_words','r')
data=open('input','a')

linecount=0

for line in image:
    if line.strip():
        linecount+=1

image.read()
image.seek(0,0)
while linecount>0:
    line1=image.readline().split()
    for entry in line1:
        num=int(entry)
        if (num<0):
            print("break from loop")
            break
        else:
            tag=words.readline()[num]
            data.write(str(tag)+' ')
    data.write('\n')
    linecount=linecount-1

data.flush()
data.close()
words.close()
image.close()
17
  • What line does the error state? Commented Jun 25, 2013 at 4:54
  • Error would be at this line tag=words.readline()[num]. 'words' file would be empty. Also, you have opened 'words' file in read-only mode and trying to write in it. Commented Jun 25, 2013 at 5:00
  • @Haidro: I used print statement, it reads the first line from image until -99. Then gives: File "get.py", line 26, in <module> tag=words.readline()[num] IndexError: string index out of range Commented Jun 25, 2013 at 5:04
  • @rajpy: you are coorect, that is the line i'm getting error at. But 'words' file is not empty. And i'm not writing in 'words' file, i'm writing to 'input' file i suppose. Commented Jun 25, 2013 at 5:07
  • 1
    @gnibbler: i think tag=words.readline() is giving an empty list here. I tried to print it. That's why the error. But i'm not sure why this is happening Commented Jun 25, 2013 at 6:04

1 Answer 1

1

You haven't told us which line fails, but this looks like the obvious one

tag = words.readline()[num]

so num is outside the bounds. I don't think it is the -99 because it should break in that case. You can add a try/except to help track it down

try:
    tmp = words.readline()
    tag = tmp[num]
except IndexError, e:
    print tmp, num

EDIT: Looks like you are mixing tabs and spaces for the indenting. This is a no-no unless you are using 8 character tab-stops

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.