Search code examples
pythonloopscsvdictionarykey

loop over keys and add missing values


I'm converting csv files into dictionaries, both csv files are similar but have different values. So one csv file has {name, state, website} and the other has {name, state, car}. The files have some of the same people in them so I want to loop over the keys and if they have the same name add the missing keys so for instance if both had a man named John Williams it would find that name and create a new dictionary saying {name: 'John Williams', state: 'FL", website "https://helpmeplease.com", car: "Kia"}. If there is a new person then I want it to create a new line and add it into the dict.

I have converted the csv files into dictionaries and I am comparing the key 'names' currently, I am stuck from here though and don't know where to go from here.

import csv
filename1 ="example.csv"
filename2 = "example2.csv"

blue = {}

with open(filename1, 'r', newline='') as f:
 for line in csv.DictReader(f):
    blue[line['name']] = line

with open(filename2, 'r', newline='') as h:
 for line in csv.DictReader(h):
    if line['name'] in blue.keys():
      #This is where I'm stuck
    else:
        blue[line['name']] = line

Solution

  • You can use dict.update to merge the contents of one dict into another. I'd also use defaultdict to manage the dict of dicts, since then you can just merge data in without having to check whether the dict needs to be created first.

    import csv
    from collections import defaultdict
    
    filenames = ["example.csv", "example2.csv"]
    
    fire = defaultdict(dict)  # type: Dict[str, Dict[str, str]]
    
    for filename in filenames:
        with open(filename, 'r', newline='') as f:
            for row in csv.DictReader(f):
                fire[row['name']].update(row)
    

    Note that if you have variables like filename1 and filename2 and copy+pasted code that does roughly the same thing to both/all of them, it's usually a good clue that you should be using a list and a for loop instead! :)