[Tutor] append index out of range

lina lina.lastname at gmail.com
Thu Jan 12 16:50:04 CET 2012


On Thu, Jan 12, 2012 at 10:56 PM, Dave Angel <d at davea.name> wrote:
> On 01/12/2012 09: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 problem has nothing to do with the size of the file, nor with append().

Thanks all.
I am really not experienced.

>
> You've got a lot going on in that line.  To solve an error message on a
> complex line, decompose it, either with extra prints, or with a literal
> decomposition into separate variables.
>
> Personally, I'd start with a print of the tobetranslatedparts variable.  it
> is a list, after all, and the error message is complaining about a list
> index.

The file I tried there is one line only one field. and the file was
still writing, not finished yet. I was so careless.

>
> Then, when it has printed out many lists, figure out what's different about
> the last one, the one that triggered the error.
>
> Once you've figured out what's wrong with that line, figure out how to deal
> with it.  Does it mean the file is invalid?  Or does it mean your loop has
> to be somewhat more complex?

The file is invalid.

Thanks again, I will try once it's finished.

>
> --
>
> DaveA
>


More information about the Tutor mailing list