我从CSV文件中提取每个国家和年份的GDP,但我对一个国家的字典每年有两个GDP



我正在使用这个 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)]}

将其他国家/地区的 GDP 相互粘贴。这段代码有什么问题?

您的问题是由于country_list中每个xoutput未清除

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']
for x in country_list:
output = []
for my_key, my_val in gdp_dict[x].items():       
try:
if my_key.isnumeric():
if maxyear >= int(my_key) >= minyear:
print(my_key)
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
d = build_plot_dict({'min_year': 2000, 'gdpfile': 'gdptable1.csv', 'country_name': 'Country Name', 'separator': ',', 'max_year': 2005, 'quote': '"', 'country_code': 'Code'}, ['Country1', 'Country2'])
for k, v in d.items():
print('{} : {}'.format(k, v))

这将显示:

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)]

此外,您的数字测试没有任何效果,需要将其转换为if语句。

最新更新