我正在尝试遍历此json字符串
"layoutOptions": {
"titleText": "Route Number",
"authorText": "West LA Yard",
"copyrightText": "",
"customTextElements": [{
"Date": "9/11/2018, 7:37:35 AM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "ParcelRouteEditingTest_1458"
}, {
"id": "ParcelRouteEditingTest_1259"
}
]
}
}
我不断遇到此错误list indices must be integers, not str
layoutOpsDict = layoutData["layoutOptions"]
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
错误: 追溯(最近一次通话):
File "<pyshell#44>", line 1, in <module>
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
TypeError: string indices must be integers, not str
除了继续设置更多变量以进行跟踪之外,在customTextElements中获取日期的最佳方法是什么?
答案 0 :(得分:1)
您正在遍历每个键,而不仅仅是"customTextElements"
,而且并非所有键都包含以"Date"
作为键的字典列表。
由于您只想浏览映射到"customTextElements"
的值,因此只能循环浏览:
dateList = [dateEle["Date"] for dateEle in layoutOpsDict["customTextElements"]]