LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   TypeError: list indices must be integers (https://www.linuxquestions.org/questions/programming-9/typeerror-list-indices-must-be-integers-852502/)

felix001 12-26-2010 07:22 PM

TypeError: list indices must be integers
 
Im just setting out on the long road to learn python so please excuse my lack of knowledge.
Im initally trying to read a csv in and print only certain "elements" (not sure if thats the right term) from a list. But I am getting the error :

TypeError: list indices must be integers

My Code is :

Code:

#!/usr/bin/python

import csv
import string

reader = csv.reader(open("statement.csv", "rb"))
for row in reader:
        print row[2,5,7]
#


Snark1994 12-27-2010 08:52 AM

You can't access a list as
Code:

print row[2,5,7]
You just need to change it to
Code:

print row[2],row[5],row[7]
And a list element is indeed the right term.

Good luck with learning Python! :)


All times are GMT -5. The time now is 01:14 AM.