Home » excel » python – Storing list into dictionary from excel

python – Storing list into dictionary from excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have stored data into list from excel but now I want to store it into dictionary.
here is the excel

in list it giving output like this.

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

same I want into dictionary. I have written this code

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]

but it is giving output like this.

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

under ‘Nodeb_IN_New’ I want all the 3 lines(from excel).

How to&Answers:

The reason your code isn’t working, is that when you are going through the rows and inserting them into the target dictionary res, you are also setting the value to a sub-dictionary, and in this sub-dictionary, the rows will always go under the same key ‘b’, ‘c’, ‘d’, etc., in other words, the rows at the bottom are overwriting the rows at top, leaving you just the 3rd row for Nodeb_IN_New.


Another way would be to set the value to a list, and go through the rows, append them to this list if they belong to the same qos, here’s the code:

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

And here’s the output of the code above:

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

Answer:

try this code :

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)

output:

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

result in json to be readable :

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