我有这样的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
我正在尝试以这种格式返回字典:
{u'Data Incomplete': {'Departments': [P41, P41, P21, P41, P01], 'Quantity': 1},
u'Typing error -> technical mix-up': {'Department': [P21, P41, P31], 'Quantity': 1}}
我正在努力获得正确的代码,尤其是获取部门列表的部分。有人可以帮帮我吗?
答案 0 :(得分: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)