简体   繁体   中英

python how to loop through csv several times from top?

I have a csv file (yy.csv) that looks like this:

term,country,score,week
aa,USA,26.17,11/22/15-11/28/15
bb,USA,16.5,11/15/15-11/21/15
cc,UK,31.36,11/22/15-11/28/15
dd,UK,21.24,11/15/15-11/21/15
ee,FR,19.2,11/22/15-11/28/15

I also have a list called country_list:

country_list=['USA','UK','FR']

I am looping through the CSV file for each country in the country list to get the term value if country in country list == col2 and week == 11/22/15-11/28/15.

Here is my code:

with open("yy.csv") as infile:
    reader = csv.reader(infile)
    next(reader, None)
    for index,country in enumerate(country_list):
        print country
        last_week_dict[country] = []
        for reader_row in reader:
            if ((reader_row[1] == country) and (reader_row[3] == "11/22/15-11/28/15")):
                last_week_dict[country].append(reader_row[0])
            else:
                continue

        print last_week_dict[country]

The output i should be getting from the print statement:

last_week_dict['USA']=['aa']
last_week_dict['UK']=['cc']
last_week_dict['FR']=['ee']

However i only got value appended to the USA key:

last_week_dict['USA']=['aa']
last_week_dict['UK']=[]
last_week_dict['FR']=[]

Could it be because when i loop through the csv file, it doesnt start from the top of the file after it goes through USA?

Your suspicion is correct: the reader object is an iterator, and iterators may only be read in the forward direction. They can't be reset.

You can avoid doing two loops from the file - it seems that your goal is to make sure that you get data for the specific countries. However, you can make sure that happens in a single loop:

import collections
import csv

# If this were a much longer collection of items it
# would be better to use a set than a list.
country_list = ['USA', 'UK', 'FR']

# The defauldict(list) allows us to just start appending
# rather than checking whether the key is already in the
# dict.
last_week_dict = collections.defaultdict(list)

with open('yy.csv') as infile:
    reader = csv.reader(infile)
    __ = next(reader)  # Skip the header
    for term, country, score, week in reader:
        if country not in country_list:
            continue
        if week != '11/22/15-11/28/15':
            continue
        last_week_dict[country].append(term)

print(last_week_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM