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