简体   繁体   English

创建嵌套的python字典

[英]Create nested python dictionary

I am using the python code below to extract some values from an excel spreadsheet and then push them to an html page for further processing. 我正在使用下面的python代码从excel电子表格中提取一些值,然后将它们推到html页面进行进一步处理。 I would like to modify the code below so that I can add additional values against each task, any help 我想修改下面的代码,以便可以为每个任务,任何帮助添加其他值

the code below does spit out the following: 下面的代码确实吐出了以下内容:

{'line items': {'AMS Upgrade': '30667', 'BMS works': '35722'}} {'订单项':{'AMS升级':'30667','BMS正常运行:'35722'}}

How can I revise the code below so that I can add 2 more values against each task ie AMS Upgrade and BMS works and get the likes of (note the structure below could be wrong) 我该如何修改下面的代码,以便可以为每个任务添加2个以上的值,即AMS升级BMS工作并得到类似的值(请注意,下面的结构可能是错误的)

{'line items': {'AMS Upgrade': {'30667','100%', '25799'}},{'BMS works': {'10667','10%', '3572'}} } {'订单项':{'AMS升级':{'30667','100%','25799'}},{'BMS有效':{'10667','10%','3572'}}}

Code: 码:

book = xlrd.open_workbook("Example - supporting doc.xls")
first_sheet = book.sheet_by_index(-1)
nested_dict = {}
nested_dict["line items"] = {}

for i in range(21,175):
    Line_items = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=8)

    if str(Line_items[0].value) and str(Line_items[1].value):
        if not Line_items[5].value ==0 : 
            nested_dict["line items"].update({str(Line_items[0].value) : str(Line_items[1].value)})

print  nested_dict
print json.dumps(nested_dict)

*** as requested see excel extract below ***根据要求,请参阅下面的excel摘录

在此处输入图片说明

In Python, each key of a dict can only be associated with a single value. 在Python中,字典的每个键只能与一个值关联。 However that single value can be a dict, list, set, etc that holds many values. 但是,单个值可以是包含许多值的字典,列表,集合等。

You will need to decide the type to use for the value associated with the 'AMS Upgrade' key, if you want it to hold multiple values like '30667','10%', '222'. 如果您希望它包含“ 30667”,“ 10%”,“ 222”等多个值,则需要确定用于与'AMS Upgrade'键关联的值的类型。

Note: what you have written: 注意:您写的内容:

{'30667','100%', '25799'}

Is a set literal in Python. 是Python中的设定文字。

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

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