Python 我从CSV文件中提取每个国家和年份的GDP,但我对一个国家的dict每年有两个GDP
我正在处理这个csv文件。我的代码是这样工作的:Python 我从CSV文件中提取每个国家和年份的GDP,但我对一个国家的dict每年有两个GDP,python,csv,dictionary,Python,Csv,Dictionary,我正在处理这个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)
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相互粘贴。此代码有什么问题?您的问题是由于未清除
国家/地区列表中每个x
的输出造成的:
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)]
国家2:[(2000,10.0),(2001,11.0),(2002,12.0),(2003,13.0),(2004,14.0),(2005,15.0)]
此外,您的数字测试没有任何效果,需要将其制作成if
语句