info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}
error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}
I have two dictionaries with username as keys. How can I merge them so that the key remains the same and both the values are assigned to the key? if the key is not present in any one of the dictionary, it should assign value 0. Actually I need to create a CSV file from the above dictionaries in the following format. "USERNAME","INFO","ERROR". Any help is appreciated. The values must not be added.
info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}
error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}
from csv import DictWriter
with open('output.csv', 'w') as f:
fields = ['USERNAME', 'INFO', 'ERROR']
writer = DictWriter(f, fields)
writer.writeheader()
all_users = list(info_dict.keys())
all_users.extend(error_dict.keys())
for user in sorted(set(all_users)):
writer.writerow({
'USERNAME': user,
'INFO': info_dict.get(user, 0),
'ERROR': error_dict.get(user, 0)
})