Home > Mobile >  updating values in dictionary when there is duplicate column
updating values in dictionary when there is duplicate column

Time:08-24

I am trying to parse my csv file into a dictionary in Python. I have two switches switch1 and switch2 which have different parameters. Switch1 has different ports which have different sent and received packets. How can I have all the switch1 parameters in key switch1 and all switch2 parameters in switch2 key. Here is my code:

def create(excel_filename):
    ctr = 0
    #excel_filename = "Switch.csv"
    switchs = {}

    with open(excel_filename, "r") as excel_csv:
        for line in excel_csv:
            if ctr == 0:
                ctr =1  # Skip the coumn header
            else:
                # save the csv as a dictionary
                switch,port,ibps,obps = line.replace(' ','').strip().split(',')
                if switch not in switchs:
                    switchs[switch] = {'port': [port], 'ibps': [ibps], 'obps': [obps]}
                else:
                    switchs[switch].update({'port': [port], 'ibps': [ibps], 'obps': [obps]})
    return switchs

s = create("machine1.csv")
print(s)

This is my csv file machine.csv

switch,port,sent,received
switch1,ge-0/1,5800,5800
switch1,ge-0/2,1000,5700
switch2,ge-0/3,2000,3000
switch2,ge-0/4,3000,4000

I want my output to be like this:

{'switch1': {'port': ['ge-0/1', 'ge-0/2'], 'sent': ['5800''1000'], 'received': ['1000','5700']}, 'switch2': {'port': ['ge-0/3','ge-0/4'], 'sent': ['2000','3000'], 'received': ['3000','4000']}}

But instead my output looks like:

{'switch1': {'port': ['ge-0/2'], 'sent': ['100000'], 'received': ['57000000']}, 'switch2': {'port': ['ge-0/4'], 'sent': ['3000000'], 'received': ['40000000']}}

What should I do to make my output look like how I want it?

CodePudding user response:

You need to append to each list in the dictionary, not update the whole dictionary. .update() doesn't concatenate the values.

                else:
                    switchs[switch]['port'].append(port)
                    switchs[switch]['ibps'].append(ibps)
                    switchs[switch]['obps'].append(obps)

CodePudding user response:

just use pandas, and you can do like this:

import pandas as pd

df = pd.read_csv('machine.csv')
d = df.groupby('switch').apply(lambda x: x.drop(columns='switch').to_dict('list')).to_dict()

>>> d
'''
{'switch1': {'port': ['ge-0/1', 'ge-0/2'],
             'sent': [5800, 1000],
             'received': [5800, 5700]},
 'switch2': {'port': ['ge-0/3', 'ge-0/4'],
             'sent': [2000, 3000],
             'received': [3000, 4000]}}
  • Related