使用OpenPyxl动态创建字典?



我使用Openpyxl读取excel文件,特别是一列,看起来像:

这个excel快照

源文档中的主类和子类的数量可以改变,我的目标是能够迭代并为表单的每个主类创建一个嵌套的字典:

main_Class1 = { 'subClass1': {'data': 'data_1'},
'subClass2': {'data': 'data_2'}}

我对任何数据类型都是开放的,只要信息是这样连接的。

我虽然有类在B列,合并主类到列A和子类到列C,然后隐藏A和C,所以我可以分开主和子更容易迭代喜欢这个

和尝试:

mainClassList = []
mainClassDict = defaultdict(list)
activeClassList=[]
for row in ws.iter_rows(min_row=2):
activeClass ="" #supposed to update this at every 
#appropriate row
if row[0].value is not None:
activeClass=row[0].value
mainClassList.append(activeClass)
mainClassDict[activeClass]=[]
activeClassList.append(activeClass)
# add 2nd column entries to 1st column keys
# would be better if these were nested dicts
if row[0].value is None and row[1].value is not None:
mainClassDict[activeClass].append(row[1].value)
#check to see things are being added and updated as needed    
print("main Class List:", mainClassList)
print("active classes;", activeClassList)
for key, value in mainClassDict.items():
print(key, ' : ', value)

我最终解决了以下问题:

mainClassDict = {} #create an empty dictionary for each level
subClassDict = {}
for row in class_sheet.iter_rows(min_row=2): 
#catch only row 1 values
if row[0].value is not None:
main = row[0].value
mainClassDict[main]={}
subClassDict[main]={}
# if none in row 1, select row 2 value instead
if row[0].value is None and row[1].value is not None: 
subclasslist = []
subclasslist.append(row[1].value)
#create empty list for data 
attributelist = [] 
# populate data list from row of choice
attributelist.append(row[...].value) 
# populate dict from elements in list using nested for
for key in subclasslist:         
for value in attributelist:
subClassDict[main][key] = value

相关内容

  • 没有找到相关文章

最新更新