FH = open ("Mylist_wincsv.csv", ' RT ') reader = Csv.reader (FH) data = List (reader) print "Data cells from CSV:" print data[0][1], data[1][1] print data[0][2], data[1][2] print data[0][3], data[1][3]
The above is the code in the book. But it can't be realized. Len (List reader) =0
Check the official website to know. Reader=csv.reader (FH)
Return a Reader object which would iterate over lines in the given csvfile. CSVFile can is any object which supports the iterator protocol and returns a string __next__() each time its Method is called- file objects and list objects are both suitable
So try to replace the code
Import Csvfh=open ("Mylist.csv", ' RT ') A=[]try: reader=csv.reader (FH) for row in reader: a.append (Row) Except Exception as E: print ("Exception is:", e) Finally: Fh.close () print (a[1][0])
That's it.
Python csv module after reader converted to list