Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

python - 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
question from:https://stackoverflow.com/questions/65849304/loop-over-keys-and-add-missing-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...