У меня есть словарь городов, полученный из файла .csv. Я пытаюсь позволить пользователям искать город, и моя программа возвращает данные для этого города. Однако я не понимаю, как написать цикл «for», который перебирает словарь. Есть совет?
Код:
import csv
#Step 4. Allow user to input a city and year
myCity = input('Pick a city: ')
myYear = ('yr'+(input('Choose a year you\'re interested in: ')))
#Step 1. Import and read CityPop.csv
with open(r'C:\Users\Megan\Desktop\G378_lab3\CityPop.csv') as csv_file:
reader = csv.DictReader(csv_file)
#Step 2. Build dictionary to store .csv data
worldCities = {}
#Step 3. Use field names from .csv to create key and access attribute values
for row in reader:
worldCities[row['city']] = dict(row)
#Step 5. Search dictionary for matching values
for row in worldCities:
if dict(row[4]) == myCity:
pass
else:
print('City not found.')
print (row)
Подробнее здесь: https://stackoverflow.com/questions/550 ... for-string