Python: How do I use DictReader twice?

后端 未结 3 939
清歌不尽
清歌不尽 2021-01-04 08:22

This feels like a very basic question, but I can\'t find any mention of it elsewhere. I\'m a beginning Python user.

When I read in data using DictReader, and then us

3条回答
  •  被撕碎了的回忆
    2021-01-04 09:19

    In Python (and almost all computer languages), if you want to store something, you have to do that explicitly. Just printing it out doesn't keep it around anywhere except on the screen.

    To use each dictionary repeatedly, but only one at a time, that's easy; row is already storing each dictionary, one at a time:

    for row in blurbs:
        print row
        print row
        print row
    

    To use all of the dictionaries repeatedly, you need to store all of them somewhere.

    They are already in blurbs, but blurbs is an iterator—something you can loop over once. Once you finish it, there's nothing left in it. That's why your second loop prints nothing.

    You want a sequence—something you can index, search, loop over dozens of times, etc. The obvious sequence type to use, when there are no special cases to worry about, is a list. So:

    with open("blurbs.csv","rb") as f:
        blurbs = csv.DictReader(f, delimiter="\t")
        rows = list(blurbs)
    
    for row in rows:
        print row
    print rows[13]
    for row in rows:
        print row
    print sorted(rows)
    

    The Tutorial section on Iterators and the following sections explain some of this.

提交回复
热议问题