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()