472,976 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

Damn error!

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
4 1084
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

56
by: john bailo | last post by:
I just installed mono from ximian on my redHat 9 workstation and wrote a simple program from the interesting book ado.net in c# by Mahesh Chand. mono is fun ( is there ado for linux ? ...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
1
by: zeny | last post by:
Hey you all, ive been confrontated with a bizarre thing: im doing a binary tree in C and in the file Tree.h i did this: //headers & other stuff typedef struct Node Node_t; struct Node{ ...
2
by: stromhau | last post by:
When compiling i get these warnings and i dont know why. Can anyone please explain ? Tommy, 1>------ Build started: Project: sdl, Configuration: Release Win32 ------ 1>Compiling......
1
by: aling | last post by:
Damn! SQLServer2000 can't add a NOT NULL COLUMN even in one empty existing table! That is, A is the existing table and it is emtpy, I want to add one NOT NULL COLUMN (col_new) to A using following...
1
by: Ian11 | last post by:
I am new to Linux and Xammp and have downloaded everthing and the server is running. I hve set up a firewall and given permisions to "permit= "80 21"" and I can access the server via localhost on the...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.