By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

Damn error!

P: n/a
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
Dec 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
One linha in tabela doesn't have the syntax "something:something" so
split() returns a 0 or 1 sized list and tmp[0] or tmp[1] points
outside the list.

2007/12/24, Vaurdan <ar**********@gmail.com>:
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
--
Florencio Cano Gabarda
Dec 24 '07 #2

P: n/a
Em 24/12/07, Vaurdan<ar**********@gmail.comescreveu:
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
'linha' doesn't have a ":" on it, so when you split you get something
like [yourlinhacontents] therefore tmp[1] is out of range.
--
http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves
Dec 24 '07 #3

P: n/a
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
Dec 24 '07 #4

P: n/a
def print_tabela(tabela):
print "Tabela 1 | Tabela 2"
for linha in tabela:
tmp = linha.split(":")
in here, insert this:
print len(tmp),
print tmp[0] + " | " + tmp[1],
I also think you may not want the trailing comma on the print line
above. Notice that, right before this,
print tmp[0] + " | " + tmp[1],
IndexError: list index out of range
it has printed something less than 2. Looks like your data isn't what
you think it is, as it doesn't appear to have a colon in one of the
lines. You might even try

for i, linha in enumerate(tabela):
tmp = linha.split(":")
if len(tmp) != 2:
print "On line %i, got [%s]" % (i+1, linha)
print tmp[0] + " | " + tmp[1]

Or, for a more compact way of writing the original code with a bit more
gracious handling:

for linha in tablea:
print " | "".join(linha.split(":")[:2])

-tkc

Dec 24 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.