python - 为什么在解析字典时出现 IndexError: list index out of range?
问题描述
我正在尝试从两个不同的字典中选择一个表,但是在下面给出的 for 循环之后,我收到一个错误为 "IndexError: list index out of range" 。
我的字典;
>>> file_dict
{'NODES': ['BSHTAS1', 'GB-vMTAS', 'GBHTAS1', 'GBMTAS1', 'GBZLRF', 'MBHTAS01', 'MNDHTAS', 'SGT-vMTAS', 'SOGZLRF'],
'KPIS': [['VoLTE MO Voice Connection Rate(%)', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Voice Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Voice Call Drop Times(times)', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Video Connection Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MO Video Answer Rate(%)', 'OK', 'OK', 'NOK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MO Video Call Drop Times(times)', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Voice Connection Rate(%)', 'NOK', 'OK', 'NOK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MT Voice Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK'],
['VoLTE MT Voice Call Drop Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Connection Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Call Drop Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK'],
['VoLTE MO Request Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Request Times(times)', 'OK', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK']
]}
我想让标题成为下面的键;
>>> file_dict["KPIS"][0][0]
'VoLTE MO Voice Connection Rate(%)'
这是我的循环。你能告诉我我的错误在哪里吗?非常感谢..
rows = []
headers = ["Kpis"]
for filename in file_list:
file_content = open('{}'.format(filename), 'r').read()
file_string = json.loads(file_content)
# convert string to dictionary
file_dict = eval(file_string)
if "KPIS" in file_dict:
ind = 0
for eachkpi in file_dict["KPIS"]:
t = [eachkpi]
for eachnode in file_dict["KPIS"]:
if eachnode[0][0] not in headers:
headers.append(eachnode[0][0])
t.append(eachnode[ind + 1])
rows.append(t)
ind = ind + 1
错误信息;
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
IndexError: list index out of range
>>>
解决方案
由于评论的快速说明;当我以转置方式执行此操作时,我没有收到错误;
rows = []
headers = ["Nodes"]
for filename in file_list:
file_content = open('{}'.format(filename), 'r').read()
file_string = json.loads(file_content)
# convert string to dictionary
file_dict = eval(file_string)
if "NODES" in file_dict:
ind = 0
for enode in file_dict["NODES"]:
t = [enode]
for ekpi in file_dict["KPIS"]:
if ekpi[0] not in headers:
headers.append(ekpi[0])
t.append(ekpi[ind + 1])
rows.append(t)
ind = ind + 1
推荐阅读
- java - 模型类在第二次使用时未将特定字符串传递给 ListViewAdapter
- javascript - 带有json数组的数据表
- sql - 一天中的持续时间 SQL
- sqlite - 哪个更适合添加视频?Firebase 或 Sqlite
- css - 从 wordpress 库中删除左边距
- python - 将时间增量列表添加到日期时间对象列表中?
- python - 使用 bs4 从 href 中提取部分文本
- karate - Karate Server feature file cannot access variables in karate-config.js file
- javascript - js函数与对象的比较
- c++ - std::out_of_range exception in string compare