Я работаю с этим https://drive.google.com/open?id=1YmKZEMpnE5V2hczZ_-vae_g6YvqPYUBE CSV-файлом.Мой код работает следующим образом:
import csv
def read_csv_as_nested_dict(filename, keyfield, separator, quote):
table = {}
with open(filename, newline='') as csvfile:
csvreader = csv.DictReader(csvfile, delimiter=separator, quotechar=quote)
for row in csvreader:
rowid = row[keyfield]
table[rowid] = row
return table
def build_plot_dict(gdpinfo,country_list):
merge = {}
gdp_dict = read_csv_as_nested_dict(gdpinfo["gdpfile"],
gdpinfo["country_name"],
gdpinfo["separator"],
gdpinfo["quote"])
maxyear = gdpinfo['max_year']
minyear = gdpinfo['min_year']
output = []
for x in country_list:
for my_key,my_val in gdp_dict[x].items():
try:
my_key.isnumeric() == True
if maxyear >= int(my_key) >= minyear:
try :
my_val == True
my_tup = int(my_key), float(my_val)
output.append(my_tup)
output.sort(key=lambda my_tup:my_tup[0])
except ValueError:
continue
except ValueError:
continue
merge[x] = output
gdp_data = []
return merge
Если я распечатаю вывод:
print(build_plot_dict({'min_year': 2000, 'gdpfile': 'gdptable1.csv', 'country_name': 'Country Name', 'separator': ',', 'max_year': 2005, 'quote': '"', 'country_code': 'Code'}, ['Country1', 'Country2']) )
При отсутствии, вернется:
{'Country1': [(2000, 1.0), (2001, 2.0), (2002, 3.0), (2003, 4.0),
(2004, 5.0), (2005, 6.0)],
'Country2': [(2000, 10.0), (2001, 11.0), (2002, 12.0), (2003, 13.0),
(2004, 14.0), (2005, 15.0)]}
Но он возвращает:
{'Country1': [(2000, 1.0), (2000, 10.0), (2001, 2.0), (2001, 11.0), (2002, 3.0),
(2002, 12.0), (2003, 4.0), (2003, 13.0), (2004, 5.0), (2004, 14.0),
(2005, 6.0), (2005, 15.0)],
'Country2': [(2000, 1.0), (2000, 10.0), (2001, 2.0), (2001, 11.0), (2002, 3.0),
(2002, 12.0), (2003, 4.0), (2003, 13.0), (2004, 5.0), (2004, 14.0),
(2005, 6.0), (2005, 15.0)]}
Вставьте ВВП из другой страны друг в друга.Что не так в этом коде?