我正在尝试迭代Excel中的列并检查字符串是否存在。如果字符串存在,我想将列表重置为[]
并重复该过程。花了太多时间在这上面,我似乎无法弄清楚我做错了什么。
示例数据:
Open Ended Schemes(Balanced)
Aditya Birla Sun Life Mutual Fund
120518 Aditya Birla Sun Life Equity Hybrid'95 Fund - Direct Plan-Dividend
120517 Aditya Birla Sun Life Equity Hybrid'95 Fund - Direct Plan-Growth
Open Ended Schemes(Debt Scheme - Banking and PSU Fund)
Axis Mutual Fund
128953 Axis Banking & PSU Debt Fund - Bonus Option
117447 Axis Banking & PSU Debt Fund - Daily Dividend Option
代码:
from openpyxl import load_workbook
import os
wb = load_workbook('m.xlsx')
ws = wb.active
keys = ['1', '2']
m_dict = {}
scheme_codes = []
for g in groups[0:2]:
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row +1, ws.max_row)):
# scheme_codes = []
for cell in row:
if cell.value != None:
if 'Schemes' in cell.value:
print('Found Schemes' + str(cell.value))
scheme_codes=[]
break
else:
scheme = cell.value
scheme_codes.append(scheme)
m_dict[g] = scheme_codes
我每个方案只获得1个项目,我尝试了各种方法,并且它只是一直在rows
。该文件有18000行。
预期输出
{1:[首次重复'计划之前的所有项目'在' A'专栏],2:[第二次重复之前的所有项目'计划'在' A'柱]
现在当我运行代码时,我得到一个len(scheme_codes)= 8069,这是我看不到的错误。第一个列表应该接近80个项目。
答案 0 :(得分:1)
这不是你要求的,它实际上提供了一些额外的信息......
它给你一个dicts的字典,它包含一组scheme_code和scheme_names元组,如:
{scheme: {sub_scheme : {(code, name), (code, name), ...}}}
如果您真的只需要顶级方案及其代码,您应该能够简化它。
只需删除一级defaultdict,然后使用scheme_codes[scheme].add(cell.value)
代替......
from openpyxl import load_workbook
import os
from collections import defaultdict
wb = load_workbook("mfcodes.xlsx")
ws = wb.active
scheme_codes = defaultdict(lambda: defaultdict(set))
scheme = 'N/A'
sub_scheme = 'N/A'
for row in ws[f'A{ws.min_row}:B{ws.max_row}']:
cell = row[0]
if not cell.value:
continue
if 'Schemes' in cell.value:
scheme = cell.value
else:
if not cell.value.isdigit():
sub_scheme = cell.value
else:
scheme_codes[scheme][sub_scheme].add((cell.value, row[1].value))
print(repr(next(iter(scheme_codes.items()))))
输出:
{'Open Ended Schemes(Balanced)' :
{'Aditya Birla Sun Life Mutual Fund': {('120518', "Aditya Birla Sun Life Equity Hybrid'95 Fund - Direct Plan-Dividend"),
('120517', "Aditya Birla Sun Life Equity Hybrid'95 Fund - Direct Plan-Growth"),
('103154', "Aditya Birla Sun Life Equity Hybrid'95 Fund - Regular Plan-Dividend"),
('103155', "Aditya Birla Sun Life Equity Hybrid'95 Fund - Regular Plan-Growth"),
('131671', 'Aditya Birla Sun Life Balanced Advantage Fund - Direct Plan - Dividend Option'),
('131670', 'Aditya Birla Sun Life Balanced Advantage Fund - Direct Plan - Growth Option'),
('131665', 'Aditya Birla Sun Life Balanced Advantage Fund - Regular Plan - Dividend Option'),
('131666', 'Aditya Birla Sun Life Balanced Advantage Fund - Regular Plan - Growth Option')},
'Baroda Pioneer Mutual Fund': {('125112', 'Baroda Pioneer Balance Fund - Plan A - Bonus Option'),
('101913', 'BARODA PIONEER BALANCE FUND - Plan A - Dividend Option'),
('101912', 'BARODA PIONEER BALANCE FUND - Plan A - Growth Option'),
('119325', 'BARODA PIONEER BALANCE FUND - Plan B (Direct) - Dividend Option'),
('119326', 'BARODA PIONEER BALANCE FUND - Plan B (Direct) - Growth Option')},
# et cetera ...
}
}
顺便说一下:第一个方案有67个代码......