List indices must be integers, not str

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

    #1

    List indices must be integers, not str

    Right I'm having a spot of trouble with integer and string values when reading from a '.txt' file.

    The aim is for the programme to read in from a file containing code similar to this...
    A;Ed;Young;e.yo ung@msn.com;223 376;1;0;1;0



    When running I receive this error code...

    Code:
    TypeError                                 Traceback (most recent call last)
    
    C:\Users\Anon\Programming Coursework Semester 2... 11-05-2010\eg3.py in <module>()
        222
        223 app = QApplication(sys.argv)
    --> 224 form = MemberListDlg(members)
        225 form.show()
        226 app.exec_()
    
    C:\Users\Anon\Programming Coursework Semester 2... 11-05-2010\eg3.py in __init__(self,
    memberList)
         53       self.listWidget.setFont(font)
         54       for m in memberList:           # Initialise the QListWidget
    ---> 55          self.listWidget.addItem(m.string())
         56
         57       # Initialise the buttons and lay them out in a vertical column
    
    
       39                 '''Return string of members'''
    ---> 40                 m = "%-2s %-16s %-16s %-16s %-16d %-16d %-16d %-16d %-16d" % (self.i_d,self.forename,self.surname,self.email,self.phone,division_current[self.division_current],self.points_current,division_previous[self.division_previous],self.points_previous)
         41                 return m
         42
    
    TypeError: list indices must be integers, not str
    WARNING: Failure executing file: <eg3.py>
    I would insert the full code but its 226 lines long. If anyone can see the answer from the run script it would be much appreciated.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Derbylad
    Right I'm having a spot of trouble with integer and string values when reading from a '.txt' file.

    The aim is for the programme to read in from a file containing code similar to this...
    A;Ed;Young;e.yo ung@msn.com;223 376;1;0;1;0



    When running I receive this error code...

    TypeError Traceback (most recent call last)

    C:\Users\Anon\P rogramming Coursework Semester 2... 11-05-2010\eg3.py in <module>()
    222
    223 app = QApplication(sy s.argv)
    --> 224 form = MemberListDlg(m embers)
    225 form.show()
    226 app.exec_()

    C:\Users\Anon\P rogramming Coursework Semester 2... 11-05-2010\eg3.py in __init__(self,
    memberList)
    53 self.listWidget .setFont(font)
    54 for m in memberList: # Initialise the QListWidget
    ---> 55 self.listWidget .addItem(m.stri ng())
    56
    57 # Initialise the buttons and lay them out in a vertical column


    39 '''Return string of members'''
    ---> 40 m = "%-2s %-16s %-16s %-16s %-16d %-16d %-16d %-16d %-16d" % (self.i_d,self. forename,self.s urname,self.ema il,self.phone,d ivision_current[self.division_c urrent],self.points_cu rrent,division_ previous[self.division_p revious],self.points_pr evious)
    41 return m
    42

    TypeError: list indices must be integers, not str
    WARNING: Failure executing file: <eg3.py>

    I would insert the full code but its 226 lines long. If anyone can see the answer from the run script it would be much appreciated.
    String formatting character %d requires an integer value. Items parsed from a file are strings until type cast to another object type. Type casting the last 5 elements of the matching tuple to int should solve your problem. Example:
    Code:
    >>> "%-2s %-16s %-16d" % ("Item 1", "Item 2", int("1234567890"))
    'Item 1 Item 2           1234567890      '
    >>>

    Comment

    Working...