2014-03-12 67 views
1

我將開始我要說的話:「我是一個使用可怕'數據庫'的noob」。下面是我的csv當前json輸出的結構(下面的輪廓)。本質上,我試圖做的是將A(信息技術)欄中的「組」添加到每個「數據」字典中,因此有一個「組」鍵:值看起來像「組」:「信息技術」。然後,第5行(可選消費品)下的所有內容都將具有「組合」:「消費者自主選擇」鍵值。CSV到JSON Python,將組類別附加到詞典

{ 
    "stocks": [ 
    { 
     "data": { 
     "portfolio_average_weight": "5.985" 
     "portfolio_total_return": "27.948" 
     }, 
     "name": "Google Inc    " 
    }, 
    { 
     "data": { 
     "portfolio_average_weight": "2.896", 
     "portfolio_total_return": "24.292" 
     }, 
     "name": "Mastercard Inc   " 
    }] 
} 

Column A       Column B   Column C  Column D 

Information Technology   [blank cell]  [blank cell]  [blank cell] 
[blank cell]      Google   5.985   27.948 
[blank cell]      Mastercard   2.896   24.292 
Consumer Discretionary   [blank cell]  [blank cell]  [blank cell] 
[blank cell]      xxxxxx   xxxxxxxxx   xxxxxxxxx 

這裏是我當前的代碼:

with open('test.csv', 'rU') as csvfile: 
    lines = csv.reader(csvfile) 
    for line in lines: 
     elif line[0] == "" and line[1] != "": 
     data = test_two_level(line) 
     bottom_level = { 
     "name": line[2], 
     "data": data} 

def test_two_level(line): 
    data = { 
     "portfolio_average_weight":line[3], 
     "portfolio_total_return":line[4]} 
    return data 

我想最終的輸出是什麼樣子:

{ 
    "stocks": [ 
    { 
     "data": { 
     "portfolio_average_weight": "5.985", 
     "portfolio_total_return": "27.948", 
     "group": "Information Technology" 
     }, 
     "name": "Google Inc    " 
    }, 
    { 
     "data": { 
     "portfolio_average_weight": "2.896", 
     "portfolio_total_return": "24.292", 
     "group": "Information Technology" 
     }, 
     "name": "Mastercard Inc   " 
    }] 
} 

下面是CSV:

Information Technology,,, 
,Google Inc    ,5.985,27.948 
,Mastercard Inc   ,2.896,24.292 
Consumer Discretionary,,, 
+0

你可以添加你的Python代碼,你正在閱讀的CSV到JSON? – Drewness

+0

因此,字典已經存在,您想通過csv文件讀取並在字典的「庫存」列表中添加一個「組名」,該列表的名稱值與「列B」匹配的csv? – martineau

+0

@martineau我想將列A中的「組」添加到每個股票的「數據」字典中。所以理想情況下,它看起來是我添加到我的原始文章底部 – user3208620

回答

1

我傾向於寧願使用csv.DictReader ov er csv.reader,因爲生成的代碼更容易閱讀,再加上將每一行讀入字典中也會使代碼更加統一—特別是在處理JSON對象時,它們本身通常由一個或多個字典組成。

import csv, json 

with open('csv_to_json_test.csv', 'rb') as csvfile: 
    csvfields = 'group', 'name', 'average_weight', 'total_return' 
    reader = csv.DictReader(csvfile, fieldnames=csvfields) 
    database = {} 
    stocks = database['stocks'] = [] # initialize item to be parsed 
    group = None 
    for row in reader: 
     if row['group']: 
      group = row['group'] 
     else: 
      stocks.append(
       { 
        'data': { 
         "portfolio_average_weight": row['average_weight'], 
         "portfolio_total_return": row['total_return'] 
        }, 
        'name': row['name'].rstrip(), # strips trailing spaces 
        'group': group, 
       } 
      ) 

print 'database =', 
print json.dumps(database, indent=4) 

輸出:

database = { 
    "stocks": [ 
     { 
      "group": "Information Technology", 
      "data": { 
       "portfolio_average_weight": "5.985", 
       "portfolio_total_return": "27.948" 
      }, 
      "name": "Google Inc" 
     }, 
     { 
      "group": "Information Technology", 
      "data": { 
       "portfolio_average_weight": "2.896", 
       "portfolio_total_return": "24.292" 
      }, 
      "name": "Mastercard Inc" 
     } 
    ] 
} 
+0

user3208620:如果這回答你的問題,請考慮接受它。如果您不熟悉這一點,請參閱SO FAQ中的[如何接受答案工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 – martineau

+0

真棒,感謝@馬蒂奧!有道理,你做到了這一點。感謝您抽出時間並保持耐心 – user3208620

+0

您可以對數據庫結構進行一項可能的改進,即將「庫存」作爲列表字典,而不是具有共同鍵值(組)的字典列表。這將使它與您的csv文件的結構更緊密地匹配。 – martineau