繁体   English   中英

为什么在解析字典时出现 IndexError: list index out of range?

[英]Why do i get IndexError: list index out of range while parsing dictionary?

我正在尝试从两个不同的字典中选择一个表,但在下面给出的 for 循环之后,我收到一个错误为“IndexError:列表索引超出范围”。

我的字典;

>>> 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

暂无
暂无

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

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