我目前正在尝试创建一个csv文件,该文件包含人们迁移到的国家/地区,原始国家和目的地的坐标以及实际迁移的人口。我能够将这些信息输出到一个文件就好了,但我应该将输出csv从最高到最低人口排序,这是不行的。我知道我必须将所有字典附加到列表中,然后通过列表对填充计数进行排序,但我似乎无法弄清楚如何进行排序本身,然后在哪里放置代码以便它仍然输出正确的信息。换句话说,如果我把排序然后写入步骤放在我的for循环中太远了我只得到津巴布韦的信息,如果我把它放在代码中再也没有完成......任何想法都会非常感激,这是我的所有相关代码:
def edges_csv(filename, orgin_column, dest_column, count_column):
edges_dict = defaultdict(list)
with open(filename, 'r', newline = '') as input_file:
# prepare to read the rows of the file using the csv packages' DictReader routines
edges_reader = csv.DictReader(input_file, delimiter=',', quotechar ='"')
for row in edges_reader:
origin = row['Country Origin Name']
dest = row['Country Dest Name']
count = row['2000 [2000]']
tuple = dest, count
edges_dict[origin].append(tuple)
for country in edges_dict:
edge_sort = sorted(edges_dict[country], key=lambda x: x[1], reverse=True)
edges_dict[country] = edge_sort
return(edges_dict)
def nodes_csv(filename, country_column, lat_column, long_column):
with open('locations.csv', 'r', newline = '') as input_file:
# prepare to read the rows of the file using the csv packages' DictReader routines
locations_reader = csv.DictReader(input_file, delimiter=',', quotechar ='"')
graph = defaultdict(list)
for row in locations_reader:
country = row['Country Name']
lat = row['Latitude']
long = row['Longitude']
tuple = lat, long
graph[country].append(tuple)
return (graph)
def main():
# open the tab-delimited input data file
with open('world_bank_country_data.txt', 'r', newline = '') as input_file:
# prepare to read the rows of the file using the csv packages' DictReader routines
country_data_reader = csv.DictReader(input_file, delimiter='\t', quotechar ='"')
with open('edges.csv', 'w', newline='') as output_file_3:
big_list = []
row_count = 0
# origin_dest_count = edges_csv('world_bank_migration.csv',
# 'origin', 'dest', 'count')
lats_longs = nodes_csv('locations.csv',
'country', 'latitude', 'longitude')
migration_outflow_graph = read_directed_graph_from_csv("world_bank_migration.csv",
"Country Origin Name", "Country Dest Name",
"2000 [2000]")
edges_writer = csv.DictWriter(output_file_3,
fieldnames=['start_country', 'end_country', 'start_lat', 'start_long', 'end_lat', 'end_long', 'count'],
extrasaction='ignore',
delimiter=',', quotechar='"')
for country in migration_outflow_graph:
edges_dict['start_country'] = country
tuple = migration_outflow_graph[country]
loc = lats_longs[country]
for item in tuple:
edges_dict['end_country'] = item[0]
edges_dict['count'] = item[1]
count_sort = edges_dict['count']
end_country = edges_dict['end_country']
dest = lats_longs[end_country]
for thing in loc:
edges_dict['start_lat'] = thing[0]
edges_dict['start_long'] = thing[1]
for thing in dest:
edges_dict['end_lat'] = thing[0]
edges_dict['end_long'] = thing[1]
# big_list = sorted(big_list, key = lambda x: x['count'], reverse = True)
# print(big_list)
# for elem in big_list:
# row_count = row_count + 1
# if row_count <= 1000:
# edges_writer.writerow(elem)
big_list.append(edges_dict)