KeyErrors while generating a dict
Below is the list of dict using trying to generate dict
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_to开发者_开发问答tal': 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
while generating below output
{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})
Getting key error,any help really appreciate
Note : Here you find two type of dicts one {'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8}, and another is {'name': u'ab', 'total': 8, 'type': u'UP'},. difference between two is keys. in first dict you find date key and another dict type key
Why shouldn't you get a KeyError? You're asking for the value associated with type
from the first row of data
and there's no such key.
EDIT:
@agf says this is underhelpful, so let me try again.
The first line of data
is
{'date': datetime.date(2011, 8, 14), 'name': u'ab', 'total': 8}
in the loop fetch
(btw, variables should be named after nouns, not verbs; function names are verbs) is set to that first row and the following line is executed:
new_y = fetch['type']
Well, that line cannot succeed. There is no entry for type
in fetch
so, as you have seen, Python throws a KeyError. Change the data so there is such an entry or change the code so it doesn't need it.
Try doing bounds checks for the "date" and "type" key:
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
A better way to do this is check that each element in the dict has all the keys you'll need.
check = ["name", "total", "user", "type", "1_total", "date"]
for fetch in data:
if all([i in fetch for i in check]):
user = fetch["name"]
. . .
精彩评论