簡體   English   中英

從Excel將列表存儲到字典中

[英]Storing list into dictionary from excel

我已經將數據存儲到excel列表中,但是現在我想將其存儲到字典中。 這是excel

在列表中,它給出這樣的輸出。

[['Nodeb_IN_New', 107, 'class-default', 'mobility-platinum', 'h1', 7, 'dscp-fc-map', ['ef']], ['', 107, '', 'mobility-gold-new', 'h2', 5, 'dscp-fc-map', ['af41']], ['', 107, '', 'mobility-silver-new', 'l1', 4, 'dscp-fc-map', ['af11', 'af21', 'af31']], ['Nokia_SRAN_S1-MME_X2_IN', 102, '', 'Nokia_SRAN_mobility_platinum', 'h1', 7, 'dscp-fc-map', ['ef', 'nc1']]]

同樣,我要進入字典。 我已經寫了這段代碼

def myfun(list):
    list = list
    res=dict()
    qos=None

    for row in list:
        if row[0]=="":
            res[qos]['b']=row[1]
            res[qos]['c']=row[2]
            res[qos]['d']=row[3]
            res[qos]['e']=row[4]
            res[qos]['f']=row[5]
            res[qos]['g']=row[6]
            res[qos]['h']=row[7]

        else:
            qos=row[0]
            res[qos]=dict()
            res[qos]['b']=row[1]
            res[qos]['c']=row[2]
            res[qos]['d']=row[3]
            res[qos]['e']=row[4]
            res[qos]['f']=row[5]
            res[qos]['g']=row[6]
            res[qos]['h']=row[7]

但它給出這樣的輸出。

{'Nodeb_IN_New': {
   'b': 107, 'c': '', 'd': 'mobility-silver-new',
   'e': 'l1', 'f': 4, 'g': 'dscp-fc-map',
   'h': ['af11', 'af21', 'af31']
    }, 
 'Nokia_SRAN_S1-MME_X2_IN': {
   'b': 102, 'c': '', 'd': 'Nokia_SRAN_mobility_platinum', 'e': 'h1', 'f': 7, 'g': 'dscp-fc-map', 'h': ['ef', 'nc1']}}

在“ Nodeb_IN_New”下,我要所有3行(來自excel)。

試試這個代碼:

import json
l=[['Nodeb_IN_New', 107, 'class-default', 'mobility-platinum', 'h1', 7, 'dscp-fc-map', ['ef']], ['', 107, '', 'mobility-gold-new', 'h2', 5, 'dscp-fc-map', ['af41']], ['', 107, '', 'mobility-silver-new', 'l1', 4, 'dscp-fc-map', ['af11', 'af21', 'af31']], ['Nokia_SRAN_S1-MME_X2_IN', 102, '', 'Nokia_SRAN_mobility_platinum', 'h1', 7, 'dscp-fc-map', ['ef', 'nc1']]]

def myfun(list):
    res=dict()
    qos=None
    d={}
    li=[]
    for row in list:
        if row[0]=="":
            res[qos]=dict()
            res[qos]['b']=row[1]
            res[qos]['c']=row[2]
            res[qos]['d']=row[3]
            res[qos]['e']=row[4]
            res[qos]['f']=row[5]
            res[qos]['g']=row[6]
            res[qos]['h']=row[7]
        else:
            qos=row[0]
            res[qos]=dict()
            res[qos]['b']=row[1]
            res[qos]['c']=row[2]
            res[qos]['d']=row[3]
            res[qos]['e']=row[4]
            res[qos]['f']=row[5]
            res[qos]['g']=row[6]
            res[qos]['h']=row[7]
        x = res.keys()
        keylist = []
        keylist.extend(iter(x))
        if keylist[0] in d.keys():
               d[keylist[0]].append(res[qos])
        else:
               d[keylist[0]] = []
               d[keylist[0]].append(res[qos])
    print(d)
    print(json.dumps(d))
myfun(l)

輸出:

{'Nokia_SRAN_S1-MME_X2_IN': [{'d': 'Nokia_SRAN_mobility_platinum', 'f': 7, 'e': 'h1', 'b': 102, 'c': '', 'h': ['ef', 'nc1'], 'g': 'dscp-fc-map'}], 'Nodeb_IN_New': [{'d': 'mobility-platinum', 'f': 7, 'e': 'h1', 'b': 107, 'c': 'class-default', 'h': ['ef'], 'g': 'dscp-fc-map'}, {'d': 'mobility-gold-new', 'f': 5, 'e': 'h2', 'b': 107, 'c': '', 'h': ['af41'], 'g': 'dscp-fc-map'}, {'d': 'mobility-silver-new', 'f': 4, 'e': 'l1', 'b': 107, 'c': '', 'h': ['af11', 'af21', 'af31'], 'g': 'dscp-fc-map'}]}

導致json可讀:

    {
  "Nokia_SRAN_S1-MME_X2_IN": [
    {
      "d": "Nokia_SRAN_mobility_platinum",
      "f": 7,
      "e": "h1",
      "b": 102,
      "c": "",
      "h": [
        "ef",
        "nc1"
      ],
      "g": "dscp-fc-map"
    }
  ],
  "Nodeb_IN_New": [
    {
      "d": "mobility-platinum",
      "f": 7,
      "e": "h1",
      "b": 107,
      "c": "class-default",
      "h": [
        "ef"
      ],
      "g": "dscp-fc-map"
    },
    {
      "d": "mobility-gold-new",
      "f": 5,
      "e": "h2",
      "b": 107,
      "c": "",
      "h": [
        "af41"
      ],
      "g": "dscp-fc-map"
    },
    {
      "d": "mobility-silver-new",
      "f": 4,
      "e": "l1",
      "b": 107,
      "c": "",
      "h": [
        "af11",
        "af21",
        "af31"
      ],
      "g": "dscp-fc-map"
    }
  ]
}

您的代碼無法正常工作的原因是,當您遍歷各行並將其插入到目標字典res ,您還將該value設置為一個子詞典,在該子詞典中,行將始終放在相同的鍵'b','c','d'等下,換句話說,底部的行覆蓋了頂部的行,僅剩下Nodeb_IN_New的第三行。


另一種方法是將value設置為列表,然后遍歷各行,如果它們屬於相同的qos ,則將它們追加到此列表中,這是代碼:

# Original excel sheet in a list
raw_list = [
    ['Nodeb_IN_New', 107, 'class-default', 'mobility-platinum', 'h1', 7, 'dscp-fc-map', ['ef']],
    ['', 107, '', 'mobility-gold-new', 'h2', 5, 'dscp-fc-map', ['af41']],
    ['', 107, '', 'mobility-silver-new', 'l1', 4, 'dscp-fc-map', ['af11', 'af21', 'af31']],
    ['Nokia_SRAN_S1-MME_X2_IN', 102, '', 'Nokia_SRAN_mobility_platinum', 'h1', 7, 'dscp-fc-map', ['ef', 'nc1']]
]

result_dict = {}
# title, will be used as key in the result_dict
title = ""

# Loop through the input list
for row in raw_list:
    # If the first value is not empty, replace the title with this new value
    if row[0] != "":
        # update title
        title = row[0]
        # Insert into the dictionary, and give an empty list as a place holder,
        #  later we can append to this list
        result_dict[title] = []
    # At this stage we can append the rest of the input row to the dictionary value
    result_dict[title].append(row[1:])

print("Result dict: ")
for (key, value) in result_dict.items():
    print("Key: {}".format(key))
    for row in value:
        print("    {}".format(row))

這是上面代碼的輸出:

Key: Nodeb_IN_New
    [107, 'class-default', 'mobility-platinum', 'h1', 7, 'dscp-fc-map', ['ef']]
    [107, '', 'mobility-gold-new', 'h2', 5, 'dscp-fc-map', ['af41']]
    [107, '', 'mobility-silver-new', 'l1', 4, 'dscp-fc-map', ['af11', 'af21', 'af31']]
Key: Nokia_SRAN_S1-MME_X2_IN
    [102, '', 'Nokia_SRAN_mobility_platinum', 'h1', 7, 'dscp-fc-map', ['ef', 'nc1']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM