CSV到字典

2024-06-01 09:18:26 发布

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

我需要一些帮助:

编写一个函数,打开文件Exports2012.csv,并返回 爱沙尼亚十大出口产品地图。地图应该 将产品名称与其对应的美元价值相关联。你知道吗

为了方便起见,应该将字符串'$2,268,911,208.49'转换为 浮动值。你知道吗

CSV示例:

#,HS,Name,Value (USD),Percent
1,8517,Telephones,"$2,823,450,843.60",15.38%
2,2710,Refined Petroleum,"$2,124,413,818.52",11.57%
3,8703,Cars,"$371,092,090.84",2.02%
4,7204,Scrap Iron,"$331,463,406.48",1.81%
5,8544,Insulated Wire,"$319,352,873.32",1.74%
6,4011,Rubber Tires,"$242,977,533.70",1.32%
7,8708,Vehicle Parts,"$241,059,109.78",1.31%
8,8429,Large Construction Vehicles,"$239,589,588.65",1.31%
9,4407,Sawn Wood,"$238,358,904.17",1.30%
10,4418,Wood Carpentry,"$237,521,163.59",1.29%
11,7210,Coated Flat-Rolled Iron,"$213,137,606.81",1.16%
12,9404,Mattresses,"$208,042,615.08",1.13%
13,4403,Rough Wood,"$206,112,209.11",1.12%
14,9403,Other Furniture,"$202,900,185.49",1.11%
15,8504,Electrical Transformers,"$202,856,149.28",1.10%

我知道怎么提取2。和3。但是我被困在这一点上了。你知道吗

import csv
f= open('EstonianExports2011.csv', 'rb')
archive = csv.reader(f, delimiter=',')
arch_dict = {}
arch_dict = {row[2]: row[3]for row in archive}
print arch_dict 

我很感激任何帮助。你知道吗


Tags: 文件csv函数字符串产品地图dictrow
2条回答

您的文件已经从最高到最低排序,因此您只需要取头后的前十行,还需要去掉$符号并替换,

import csv
with open('EstonianExports2011.csv', 'rb')as f:
    archive = list(csv.reader(f, delimiter=','))[1:11] # get lines 1 to 10
    arch_dict = {row[2]: float(row[3].strip("$").replace(",","")) for row in archive}

arch_dict
{'Rubber Tires': 242977533.7, 'Cars': 371092090.84, 'Vehicle Parts': 241059109.78, 'Insulated Wire': 319352873.32, 'Scrap Iron': 331463406.48, 'Telephones': 2823450843.6, 'Sawn Wood': 238358904.17, 'Large Construction Vehicles': 239589588.65, 'Wood Carpentry': 237521163.59, 'Refined Petroleum': 2124413818.52}

In [2]: s = "$213,137,606.81"

In [3]: s.strip("$") # strips from the ends of a string
Out[3]: '213,137,606.81'

In [5]: s.strip("$").replace(",","") # combine with replace to remove the commas
Out[5]: '213137606.81'

把它变成一个函数应该很简单。你知道吗

因为这是一个赋值,所以我不会(至少最初)提供显式代码,而是为算法提供建议:

  1. product = row[2]valuestring = row[3]在你的文件中
  2. 只取valuestring中第一个和最后一个字符之间的部分
  3. 从经过修剪的valuestring中删除逗号
  4. 转换为浮点数
  5. 将其保存到成对的列表(乘积和值),可能使用zip函数
  6. 使用字典理解中的函数sorted(zipped_list, key=lambda l:l[1]制作你的字典,就像你现在的字典一样。你知道吗

相关问题 更多 >