简体   繁体   English

XLSXwriter-编写多个嵌套字典

[英]XLSXwriter - writing multiple nested dictionaries

I need to write a multi-nested dictionary to an Excel file. 我需要将多嵌套词典写到Excel文件中。 The dictionary is structured as dict1:{dict2:{dict3:value}}} . 字典的结构为dict1:{dict2:{dict3:value}}} My third write loop is raising a keyerror: '' even though there ought not be any blank keys. 我的第三个写循环正在引发一个keyerror: ''即使不应有任何空白键。

I attempted to use this abhorrent monster as it has worked wonderfully for smaller dictionaries, but 1) there has to be a better way 2) I'm unable to scale it for this dictionary... 我尝试使用这个可恶的怪物,因为它在较小的词典中表现出色,但是1)必须有更好的方法2)我无法为这本词典缩放比例...

import xlsxwriter
workbook = xlsxwriter.Workbook('datatest.xlsx')
worksheet = workbook.add_worksheet('test1')                            
row = 0
col = 0
for key in sam_bps.keys():
    row += 1
    worksheet.write(row, col, key)
    for key in sam_bps[sam].keys():
        row, col = 0,1
        worksheet.write(row,col,key)
        row += 1
        for key in sam_bps[sam][bp].keys():
            row,col = 0,2
            worksheet.write(row,col,key)
            row += 1
            for key in sam_bps[sam][bp][mpn].keys():
                row,col = 0,3
                worksheet.write(row,col,key)
                row += 1
                for item in sam_bps[sam][bp][mpn].keys():
                    row,col = 0,4
                    worksheet.write(row,col,item)
                    row += 1
workbook.close()

I've also considered converting the dictionary to a list of tuples or list of lists, but it doesn't output how I need. 我也考虑过将字典转换为元组列表或列表列表,但是它不会输出我所需要的。 And it'll probably cost more time to split those open afterwards anyway. 无论如何,可能会花费更多时间将这些内容拆分开。

Here's the code for the dicionary: 这是字典的代码:

sam_bps = {}
sam_bps_header = ['SAM','BP','MPN','PLM_Rate']
for row in plmdata:
    sam,mpn,bp,doc = row[24],row[5],row[7],row[2]
    if sam == '':
        sam = 'Unknown'
    if doc == 'Requirement':
        if sam not in sam_bps:
            sam_bps[sam] = {bp:{mpn:heatscores[mpn]}}
        elif bp not in sam_bps[sam]:
            sam_bps[sam][bp] = {mpn:heatscores[mpn]}
        elif mpn not in sam_bps[sam][bp]:
            sam_bps[sam][bp][mpn] = heatscores[mpn]
print(sam_bps['Dan Reiser'])

EDIT: Added print statement to show output per feedback 编辑:添加了打印语句以显示每个反馈的输出

{'Fortress Solutions': {'MSM5118160F60JS': 45}, 'Benchmark Electronics': {'LT1963AES8': 15}, 'Axxcss Wireless Solutions Inc': {'MGA62563TR1G': 405}} {'Fortress Solutions':{'MSM5118160F60JS':45},'Benchmark Electronics':{'LT1963AES8':15},'Axxcss Wireless Solutions Inc':{'MGA62563TR1G':405}}

I'd like to see this output to an Excel file, with the first column of course being the first key of sam_bps 我希望看到此输出到Excel文件,当然第一列是sam_bps的第一把钥匙

Your question would be easier to answer if you provided an example of the dictionary you are trying to save. 如果您提供了要保存的词典的示例,则将更容易回答您的问题。

Have you considered just serializing/deserializing the dictionary to JSON format? 您是否考虑过仅将字典序列化/反序列化为JSON格式?

you can save/re-load the file with minimal code: 您可以使用最少的代码保存/重新加载文件:

import json
data = {'test': {'test2': {'test3':2}}}
with open('data.json', 'w') as outfile:
    json.dump(data, outfile)

with open('data.json') as data_file:
    data = json.load(data_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM