我正在处理这个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从其他国家/地区粘贴。这段代码有什么问题?
答案 0 :(得分:0)
您的问题是由于output
中的x
country_list
未被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
此外,您的数字测试没有任何效果,需要将其设为{{1}}语句。