带变量的嵌套Python字典
编写此代码的原因:我试图创建一个脚本,从excel中提取数据并将其放入PowerPoint。它应该检测选项卡颜色,以指示幻灯片上是否出现多个图表 到目前为止,数据似乎被适当地拉入了data_dict变量,本应在同一张幻灯片上的图表并不都被拉入,只有最后的图表数据才被拉入了slide_数据字典带变量的嵌套Python字典,python,python-2.7,if-statement,Python,Python 2.7,If Statement,编写此代码的原因:我试图创建一个脚本,从excel中提取数据并将其放入PowerPoint。它应该检测选项卡颜色,以指示幻灯片上是否出现多个图表 到目前为止,数据似乎被适当地拉入了data_dict变量,本应在同一张幻灯片上的图表并不都被拉入,只有最后的图表数据才被拉入了slide_数据字典 slidecount = 0 for wksht in wb.sheetnames: combinecount = 1 if tabcolor is not None:
slidecount = 0
for wksht in wb.sheetnames:
combinecount = 1
if tabcolor is not None:
if tabcolor == most_recent_tabcolor:
combinecount += 1
slidecount -= 1
else:
combinecount = 1 # Resets to one
most_recent_tabcolor = tabcolor
else:
combinecount = 1 # Resets to one
'''
Placed after combinecheck para place correctly in dictionary
'''
infolist = [intended_chart, data_question, data_base, forcefloat, forcepercent]
slide_data[slidecount] = {}
slide_data[slidecount][combinecount] = {'Chart Data': data_dict, 'Chart Info': infolist}
slidecount += 1
我期望得到以下结果:
0,{1:{‘图表数据’…}2:{‘图表数据’…}3:{‘图表数据’…}
我得到:
0,{3:{'Chart Data'…}只有在选项卡颜色不是“无”时,slidecount始终为0。该变量没有增量,只有在tab color为none时递减
我想将此作为评论发布,但我没有足够的声誉来这样做。所以问题在于命令:
slide_data[slidecount] = {}
它删除了slidecount中的所有数据,但我需要有类似的东西来实际添加更多到嵌套字典中。我在wkshts的迭代中创建了以下内容:
dictcount = 0
while dictcount <= len(wb.sheetnames):
slide_data[dictcount] = {}
dictcount += 1
dictcount=0
而dictcount“slidecount=0”应该在“for”循环之前初始化谢谢。我的示例不正确,但代码中的示例是正确的。我已经更新了问题。inflolist是什么?您不应该将slide\u数据[slidecount]={}
移出循环吗??在循环的每个迭代中,您都会覆盖幻灯片数据[slidecount],而不是实现它。infolist是与图表相关的非数据项列表,必须插入到字典中,以便以后可以正确导入图表。