2011-08-19 23 views
1

下面是字典的使用試圖生成字典KeyErrors而產生的字典

data = [ 
    {'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 15), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 16), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 17), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 18), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 19), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 20), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 21), 'name': u'ab', 'total': 8}, 
    {'date': datetime.date(2011, 8, 18), 'name': u'aj', 'total': 0}, 
    {'date': datetime.date(2011, 8, 14), 'name': u'ap', 'total': 8}, 
    {'date': datetime.date(2011, 8, 15), 'name': u'ap', 'total': 8}, 
    {'date': datetime.date(2011, 8, 16), 'name': u'ap', 'total': 8}, 
    {'date': datetime.date(2011, 8, 17), 'name': u'ap', 'total': 8}, 
    {'name': u'ab', 'l_total': 8, 'type': u'UP'}, 
    {'name': u'ab', 'l_total': 8, 'type': u'PL'}, 
    {'name': u'fk', 'l_total': 29, 'type': u''}, 
    {'name': u'jw', 'l_total': 1, 'type': u'PD'}, 
    {'name': u'uk', 'l_total': 16, 'type': u'UP'}, 
    {'name': u'sk', 'l_total': 24, 'type': u'PL'}, 
    {'name': u'ss', 'l_total': 8, 'type': u'PL'}, 
    {'name': u'sst', 'l_total': 8, 'type': u'PL'}] 



results = collections.defaultdict(dict) 

for fetch in data: 
     user = fetch['name'] 
     hrs = fetch['total'] 
     row = results[user] 
     new_y = fetch['type'] 
     row['new_y'] = fetch['l_total'] 
     row['user'] = user 
     dt_string = str(fetch['date']) 
     if dt_string in fetch and row[dt_string] != hr: 
      raise Exception, "Contradiction: '%s' on '%s'" % (user, dt_string) 
     row[dt_string] = hrs 
     row.setdefault('Total', 0) 
     row['Total'] += hrs 

同時產生以下輸出

{u'ap': {'Total': 32, 'user': u'ap', '2011-08-17': 8, 
     '2011-08-16': 8, '2011-08-15': 8, '2011-08-14': 8, 
     'up': 8, 'PL':8}) 

獲取鍵錯誤列表,任何幫助,真的很感謝

注意:在這裏你可以找到兩種類型的字典:{'date':datetime.date(2011,8,14),'name':u'ab','total':8},另一種是{'name' :你好, 'total':8,'type':u'UP'} ,.兩者之間的區別是關鍵。在第一個快譯通,你找到日期鍵和另一個字典式按鍵

回答

6

爲什麼不應你得到一個KeyError異常?您要求從data的第一行中獲得與type相關聯的值,並且沒有這樣的密鑰。

編輯:

@agf說,這是underhelpful,所以讓我再試一次。

data第一行是

{'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8} 
在循環 fetch

(順便說一句,變量應該是名詞來命名,而不是動詞;函數名是動詞)設置爲第一行和下面一行執行:

new_y = fetch['type'] 

那麼,該行不能成功。在fetch中沒有type的條目,所以,正如你所看到的,Python會拋出一個KeyError。更改數據,以便有這樣的條目或更改代碼,因此它不需要它。

+0

但這裏爲兩個類型類型的字典是有一個是{ '日期':datetime.date(2011,8,14)中, '名稱':u'ab」, '總計':8}另一個是{ 'name':u'sst','total':8,'type':u'PL'},意思是在一個字典中你會找到日期鍵,另一個是鍵入鍵。 – sush

+0

有兩種類型的詞典 - 第一種程序失敗,從未到達第二種類型。如果你的食物「只有一半」中毒了,你會期望完成你的盤子嗎?我想也許你只是想製作兩份名單,一份用於每種字典,並分別處理這些名單。 – Malvolio

+0

@ malvolio,謝謝我分離和合並輸出。 – sush

0

嘗試做邊界檢查的「日期」和「輸入」鍵:

for fetch in data: 
    if "date" in fetch and "type" in fetch: 
     user = fetch['name'] 
     hrs = fetch['total'] 
     row = results[user] 
     new_y = fetch['type'] 
     row['type'] = fetch['l_total'] 
     row['user'] = user 
     dt_string = str(fetch['date']) 
     if dt_string in fetch and row[dt_string] != hr: 
      raise Exception, "Contradiction: '%s' on '%s'" % (user, dt_string) 
     row[dt_string] = hrs 
     row.setdefault('Total', 0) 
     row['Total'] += hrs 

一個更好的辦法來做到這一點是檢查在字典每個元素有所有你需要的按鍵。

check = ["name", "total", "user", "type", "1_total", "date"] 
for fetch in data: 
    if all([i in fetch for i in check]): 
     user = fetch["name"] 
     . . . 
+0

謝謝你的幫助lysdexia,但我沒有得到行[new_y] = fetch ['l_total'],這個值沒有得到添加到字典 – sush