[英]Why do i get IndexError: list index out of range while parsing dictionary?
I am trying to pick a table from two different dictionary but after the for loop given below, i get an error as "IndexError: list index out of range".我正在尝试从两个不同的字典中选择一个表,但在下面给出的 for 循环之后,我收到一个错误为“IndexError:列表索引超出范围”。
My dictionary;我的字典;
>>> 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']
]}
And i would like to make the headers the keys below;我想让标题成为下面的键;
>>> file_dict["KPIS"][0][0]
'VoLTE MO Voice Connection Rate(%)'
Here is my loop.这是我的循环。 Could you please tell me where is my error?
你能告诉我我的错误在哪里吗? Thanks a lot..
非常感谢..
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
The error message;错误信息;
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
IndexError: list index out of range
>>>
A quick note due to the comments;由于评论的快速说明; I do not receive error when i do this in transposed way;
当我以转置方式执行此操作时,我没有收到错误;
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.