Python: How do I use DictReader twice?

后端 未结 3 940
清歌不尽
清歌不尽 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 08:57

    If you want to re-use the reader, you could seek the file back to 0. But, if the first row int the csv are headings, then that will be part of the output:

    >>> f = open( 'file.csv', 'rbU' )
    >>> reader = csv.DictReader( f )
    >>> reader.next()
    {'col1': '6', 'col2': '0.9', 'col3': '8'}
    >>> f.seek(0)
    >>> reader.next()
    {'col1': 'col1', 'col2': 'col2', 'col3': 'col3'}
    >>> f.close()
    

    DictReader uses the first row as the dictionary keys (if they are not otherwise supplied). Creating a new reader object is a lot simpler. You can also copy the data into a data structure like a list and loop over that.

    0 讨论(0)
  • 2021-01-04 09:15

    You just need to seek the file back to the start:

    with open("blurbs.csv","rb") as f:
        blurbs = csv.DictReader(f, delimiter="\t")
        for row in blurbs:
            print row
        f.seek(0)
        for row in blurbs:
            print row
    

    Alternatively you can wrap the dictionary generation into a list of dicts and operate on that:

    with open("blurbs.csv","rb") as f:
        blurbs = list(csv.DictReader(f, delimiter="\t"))
    for row in blurbs:
        print row
    for row in blurbs:
        print row
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题