[Tutor] Enumerate vs DictReader object manipulation:

Alan Gauld alan.gauld at btinternet.com
Wed Feb 3 11:24:05 EST 2016


On 03/02/16 15:29, Ek Esawi wrote:

>     reader = csv.DictReader(MyFile)
> 
>     for row in reader:
>         list_values = list(row.values())
>         print (list_values)
> 

At this point the reader has reached the end of the file.

>     for i,j in enumerate(reader):
>         print(j)

So you can't read any more using that same reader you
need to create a new reader. (Or reset the old one, but
I don't think that's possible)

But why are you using a DictReader when you immediately
convert the data into a list? Why not use the standard
reader which returns tuples. Or why not just use the dicts
that you have - usually that leads to more intuitive
and reliable code since you access the data by name
rather than  column number.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list