[Tutor] append index out of range

bob gailer bgailer at gmail.com
Thu Jan 12 15:48:33 CET 2012


On 1/12/2012 9:38 AM, lina wrote:
> Hi,
>
> there is a file
>
> $ cat atom-pair_9.out | wc -l
> 75426
>
> there is 75426 lines there,
>
>      results=[]
>      unique={}
>      for line in open(tobetranslatedfile,"r"):
>          tobetranslatedparts=line.strip().split()
>          results.append(dictionary[tobetranslatedparts[2]])
>
> it complains
>
>      results.append(dictionary[tobetranslatedparts[2]])
> IndexError: list index out of range
>
>
> is it really too large this file?

This has nothing to do with the size of the file. It means Python 
encountered a line in the file with less than 3 
"whitespace-separated-tokens".

List indexing starts at 0, so tobetranslatedparts[2] refers to the 3rd 
item of the list that was created by split().

If that does not help, please show us some sample lines.




-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list