Stack Exchange
KeyError: 'name' Why can't I use 'name'?
I wanna make a dictionary has name's key & data.In views.py I wrote
data_dict ={}
def try_to_int(arg):
    try:
        return int(arg)
    except:
        return arg

def main():
    book4 = xlrd.open_workbook('./data/excel1.xlsx')
    sheet4 = book4.sheet_by_index(0)

    data_dict_origin = OrderedDict()
    tag_list = sheet4.row_values(0)[1:]
    for row_index in range(1, sheet4.nrows):
        row = sheet4.row_values(row_index)[1:]
        row = list(map(try_to_int, row))
        data_dict_origin[row_index] = dict(zip(tag_list, row))

    if data_dict_origin['name'] in data_dict:
        data_dict[data_dict_origin['name']].update(data_dict_origin)
    else:
        data_dict[data_dict_origin['name']] = data_dict_origin

main()

When I printed out data_dict,it is
OrderedDict([(1, {'user_id': '100', 'group': 'A', 'name': 'Tom', 'dormitory': 'C'}), (2, {'user_id': '50', 'group': 'B', 'name': 'Blear', 'dormitory': 'E'})])

My ideal dictionary is
dicts = {
    Tom: {
        'user_id': '100',
        'group': 'A', 
        'name': 'Tom',
        'dormitory': 'C'
    },
    Blear: {
    },
}

How should I fix this?What should I write it?

why first time you use Tom and next 2 ?

@BearBrown Sorry,it is mistaken.I edited my question.

The code is using the wrong key in the dictionary. The keys are 1, 2, and do not have the name key. You can use this code instead:
for value in data_dict.values():
    if value['name'] in data_dict:
        data_dict[value['name']].update(value)
    else:
        data_dict[value['name']] = value


thx,ur answer.i wrote ur codes,but {} was printed out.I think  maybe data_dict.values() is data_dict_origin.values() .How should I do it?

Your data_dict_origin has numbers as keys and dicts as values (which technically makes it a sparse array of dicts). The "name" key exists in those dicts, not in your data_dict.

@user8504021 please avoid the childish spelling, it's not hoing to make you look smart here. And SO is not a free gimme-da-code-plz service either FWIW.
