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

2024-09-24 06:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理这个https://drive.google.com/open?id=1YmKZEMpnE5V2hczZ_-vae_g6YvqPYUBEcsv文件。我的代码是这样工作的:

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。这个代码有什么问题?你知道吗


Tags: csvkeyforoutputmyastableyear
1条回答
网友
1楼 · 发布于 2024-09-24 06:21:58

您的问题是由于output没有为country_list中的每个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)]
Country2 : [(2000, 10.0), (2001, 11.0), (2002, 12.0), (2003, 13.0), (2004, 14.0), (2005, 15.0)]

而且,您的数字测试没有任何效果,需要将它转换成if语句。你知道吗

相关问题 更多 >