Damn error!

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 24 07:49:42 EST 2007


On Mon, 24 Dec 2007 04:30:50 -0800, Vaurdan wrote:

> Hello,
> I've this code:
> def print_tabela(tabela):
> 	print "Tabela 1 | Tabela 2"
> 	for linha in tabela:
> 			tmp = linha.split(":")
> 			print tmp[0] + " | " + tmp[1],
> 
> But give me this error:
> Tabela 1 | Tabela 2
> Traceback (most recent call last):
>   File "./teste.py", line 126, in <module>
>     print_tabela(conteudo)
>   File "./teste.py", line 58, in print_tabela
>     print tmp[0] + " | " + tmp[1],
> IndexError: list index out of range


You've asked for items 0 and 1 of the list tmp. One of those indexes is 
out of range. That most likely means you have a line with no ":" in it, 
so tmp only has one item, not two.

You can see that for yourself by putting a line "print tmp" just after 
the split().

You might also like to consider doing this:


def print_tabela(tabela):
    print "Tabela 1 | Tabela 2"
    for linha in tabela:
        print linha.replace(":", " | ")



Hope this helps,



-- 
Steven



More information about the Python-list mailing list