2011-08-19 43 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