2014-10-27 54 views
0

我需要一些幫助的assiment:CSV到字典

寫,打開該文件Exports2012.csv,並返回一個 地圖愛沙尼亞出口中10頂級產品的功能。地圖應該 將產品名稱與其對應的USD值相關聯。

爲方便起見,您應該將字符串'$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 

我很感激任何幫助。

+0

不相關,但賦值爲「寫入函數」。你最終應該寫這個函數,而不是一個純腳本; o) – heltonbiker 2014-10-27 23:08:05

+0

看看這裏:http://stackoverflow.com/questions/20304824/sort-dict-by-highest-value – thefragileomen 2014-10-27 23:19:23

回答

1

您的文件已經排序從最高到最低,所以你只需要採取的第一個十行頭之後,還需要剝離$標誌和更換,的:

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' 

把它作爲一個功能應該是非常簡單的。

0

因爲這是一個任務,我不會(至少在初期)提供明確的代碼,而是一個算法的建議:

  1. product = row[2]和文件valuestring = row[3];
  2. 只取第一個字符和最後一個字符之間的部分valuestring;
  3. 刪除修剪後的逗號valuestring;
  4. 轉換爲浮點數;
  5. 將其保存到配對(產品和值)列表中,可能使用zip函數;
  6. 使用字典理解中的函數sorted(zipped_list, key=lambda l:l[1]來製作您的字典,就像您當前的那樣。