将列表从ex存储到字典中

2024-05-20 17:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经将数据从excel存储到列表中,但现在我想将其存储到字典中。 here is the 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\u IN\u New”下,我需要所有3行(来自excel)。你知道吗


Tags: inmapnewresexcellistrowfc
2条回答

请尝试以下代码:

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

相关问题 更多 >