从Excel将列表存储到字典中

时间:2019-06-23 20:27:39

标签: python excel list dictionary jinja2

我已经将数据存储到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_IN_New”下,我希望所有3行(来自excel)。

2 个答案:

答案 0 :(得分:1)

尝试此代码:

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"
    }
  ]
}

答案 1 :(得分:1)

您的代码无法正常工作的原因是,当您遍历各行并将其插入目标字典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']]