473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

List index out of range

7 New Member
After starting my coursework I've come across the following error code... "List index out of range". I was expecting this to be something very simple that I've done wrong and still imagine it will be seeing as I've written code similar to this before.

The code is designed to turn a list of players/members into separate strings. The code is shown below...

Expand|Select|Wrap|Line Numbers
  1. class Member(object):
  2.     '''a member'''
  3.  
  4. fin = open ("players.txt","r")
  5.  
  6. l= []
  7. for line in fin:
  8.     line=line.strip()
  9.     x = line.split(";")
  10.     memb = Member()
  11.     memb.id= x[0]
  12.     memb.forename= x[1]
  13.     memb.surname= x[2]
  14.     memb.email= x[3]
  15.     memb.phone= x[4]
  16.     memb.divc= x[5]
  17.     memb.pointc= x[6]
  18.     memb.divp= x[7]
  19.     memb.pointp= x[8]
  20.     l.append(memb)
  21. fin. close ()
  22.  
  23. print l
A sample of the players.txt file is...

A;Ed;Young;e.young@lboro.ac.uk;223376;1;0;1;0
B;Gordon;McTaggart-Cowan;g.b.mctaggart@lboro.ac.uk;226276;1;0;1;0
C;Tom;Kane;t.g.kane@lboro.ac.uk;726653;1;0;1;0
D;Chris;Holmes;c.r.holmes@lboro.ac.uk;225378;1;0;1 ;0
E;David;Mulvaney;d.k.mulvaney@lboro.ac.uk;227142;1 ;0;1;0
F;James;Buller;j.buller@lboro.ac.uk;223543;1;0;1;0
A;Andrew;Wright;a.wright@lboro.ac.uk;218432;2;0;2; 0

If someone's able to shed a bit of light on why I'm getting this error code I'll be thankful as I'm not back at university until Monday.
May 7 '10 #1
4 4403
bvdet
2,851 Recognized Expert Moderator Specialist
It could be something as simple as an extra line in the data file. An extra line is created when the last full line ends with "\n".
Expand|Select|Wrap|Line Numbers
  1. class Member(object):
  2.     '''a member'''
  3.  
  4. fin = open ("players.txt","r")
  5.  
  6. attrs = ["id", "forename", "surname", "email", "phone", "pointc", "divp", "pointp"]
  7.  
  8. l= []
  9. for line in s.split("\n"):
  10.     line=line.strip()
  11.     if line:
  12.         memb = Member()
  13.         for i, attr in enumerate(attrs):
  14.             setattr(memb, attr, line[i])
  15.         l.append(memb)
  16. fin. close ()
  17.  
  18. print l
May 7 '10 #2
woooee
43 New Member
You will have to post the error message which lists the line that contains the error. My guess is that you have a record with less than 9 items.
May 8 '10 #3
Derbylad
7 New Member
Thanks for the quick responses guys, Turned out there was an extra new line at the end of the data code.
Much appreciated.
May 8 '10 #4
woooee
43 New Member
Whenever you are doing anything similar, always check for the proper length and print if it's not correct. You will eliminate a lot of unnecessary debugging.
Expand|Select|Wrap|Line Numbers
  1. x = line.split(";")
  2. if len(x) > 8:
  3.     ## process normally
  4. else:
  5.     print("Bogus line length =", x) 
May 8 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
5
by: questions? | last post by:
I want to do list index function. >>> y= >>> y >>> y.index Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsubscriptable object It works with y=. Anyone has...
9
by: William Meyer | last post by:
hi, I need to get the index of an object in a list. I know that no two objects in the list are the same, but objects might evaluate as equal. for example list = for object in list:...
8
by: sam | last post by:
hey everybody, this is my first time posting here. i'm pretty new to python and programming in general (as you'll soon work out for yourselves...) i'm trying to code a version of a selection...
35
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
11
by: Steve | last post by:
I'm trying to create a list range of floats and running into problems. I've been trying something like: a = 0.0 b = 10.0 flts = range(a, b) fltlst.append(flts)
7
by: python101 | last post by:
My code s= for k in range(len(s)): st=s.count(s) if st>=2: print s.pop(s.index(s)) Traceback (most recent call last):
1
by: ApoorvaDesai | last post by:
How do I check if a certain column exists in the CSV file, from the following code I try to check if s exists. It gives me an error INDEX ERROR: List index out of range. If its Null I expect it to...
3
by: Riccardo Murri | last post by:
Hello, I have some code that stops when trying to find a graph in a list of similar graphs:: (Pydb) list 110 try: 111 canonical = self.base 112 except ValueError: 113 ...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.