List index out of range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Derbylad
    New Member
    • May 2010
    • 7

    List index out of range

    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...

    Code:
    class Member(object):
    	'''a member'''
    
    fin = open ("players.txt","r")
    
    l= []
    for line in fin:
    	line=line.strip()
    	x = line.split(";")
    	memb = Member()
    	memb.id= x[0]
    	memb.forename= x[1]
    	memb.surname= x[2]
    	memb.email= x[3]
    	memb.phone= x[4]
    	memb.divc= x[5]
    	memb.pointc= x[6]
    	memb.divp= x[7]
    	memb.pointp= x[8]
    	l.append(memb)
    fin. close ()
    
    print l
    A sample of the players.txt file is...

    A;Ed;Young;e.yo ung@lboro.ac.uk ;223376;1;0;1;0
    B;Gordon;McTagg art-Cowan;g.b.mctag gart@lboro.ac.u k;226276;1;0;1; 0
    C;Tom;Kane;t.g. kane@lboro.ac.u k;726653;1;0;1; 0
    D;Chris;Holmes; c.r.holmes@lbor o.ac.uk;225378; 1;0;1;0
    E;David;Mulvane y;d.k.mulvaney@ lboro.ac.uk;227 142;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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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".
    Code:
    class Member(object):
    	'''a member'''
    
    fin = open ("players.txt","r")
    
    attrs = ["id", "forename", "surname", "email", "phone", "pointc", "divp", "pointp"]
    
    l= []
    for line in s.split("\n"):
        line=line.strip()
        if line:
            memb = Member()
            for i, attr in enumerate(attrs):
                setattr(memb, attr, line[i])
            l.append(memb)
    fin. close ()
    
    print l

    Comment

    • woooee
      New Member
      • Mar 2008
      • 43

      #3
      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.

      Comment

      • Derbylad
        New Member
        • May 2010
        • 7

        #4
        Thanks for the quick responses guys, Turned out there was an extra new line at the end of the data code.
        Much appreciated.

        Comment

        • woooee
          New Member
          • Mar 2008
          • 43

          #5
          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.
          Code:
          x = line.split(";")
          if len(x) > 8:
              ## process normally
          else:
              print("Bogus line length =", x)

          Comment

          Working...