从 excel table 在 Python 中获取嵌套字典

Getting a nested dictionary from excel table in Python

我有一个这样的 excel 数据:

    Dep request date Code   Reason
    P41 15.02.2018  0060    Data Incomplete
    P41 02.02.2018  0060    Data Incomplete
    P21 11.01.2018  0060    Data Incomplete
    P41 14.02.2018  0060    Data Incomplete
    P01 13.03.2018  0060    Data Incomplete
    P21 09.02.2018  0030    Typing error -> technical mix-up
    P41 07.02.2018  0030    Typing error -> technical mix-up
    P31 28.02.2018  0030    Typing error -> technical mix-up

这是我的代码:

def get_reasons(readfilename):
    act_sheet = read_excelfile(readfilename)
    deps = []
    reasons = []
    item_dict = {}
#    create a list of uppercase letters for A-Z
    col_header = [chr(one).upper() for one in range(97,123)]

    for idx, header in enumerate(col_header):
        head = header + str(1)

        if act_sheet[head].value == 'Dep':
            for j in range(2, act_sheet.max_row+1):
                deps.append(act_sheet[header + str(j)].value)

        if act_sheet[head].value == 'Reason':
            for m in range(2, act_sheet.max_row+1):
                items = act_sheet[header + str(m)].value
                reasons.append(items)           
                item_dict.setdefault(items, {})

                item_dict[items].setdefault('Departments', deps)

    amounts = Counter(reasons) 
    for k,v in amounts.items():
        item_dict[k]['Quantity'] = v

    return item_dict

我正在尝试 return 这种格式的字典:

{u'Data Incomplete': {'Departments': [P41, P41, P21, P41, P01], 'Quantity': 1},
 u'Typing error -> technical mix-up': {'Department': [P21, P41, P31], 'Quantity': 1}}

我正在努力获取正确的代码,尤其是获取部门列表的部分。有人可以帮我吗?

最好的方法是使用数据库。但是,对于单次使用,使用 openpyxl 很容易做到这一点。您真的应该更仔细地研究文档中的示例,这样您就不会像现在那样编写冗长的代码,这会让您很难准确理解您要做什么。

以下内容对您有所帮助。

headers = {c.value:c.col_idx for c in ws[1]}
reason_col = headers['Reason'] - 1
dep_col = headers['Dep'] - 1

reasons = defaultdict(set)

for row in ws.iter_rows(min_row=2):
    reason = row[reason_col].value
    dep = row[dep_col].value
    reasons[reason].add(dep)