I have a dictionary & the values are also a dictionary itself- how do I write the values to a CSV, with the key in the values being the column name?

Question:

Writing this out is a little confusing but it’ll be easier to see with my code.I’m not sure if my title accurately describes my issue, I’m a newbie and I’m struggling to wrap my head around this.

main.py (part 1)

new_dict = {}
for ip, host, os, vuln_title, vuln_id, cvss2, cvss3, descr, proof, solu, cves in data[1:]:
    vuln_data = new_dict.setdefault(vuln_id, {"Vulnerability ID": vuln_id, "Name": host, "Affected IP Address": [], "Risk Exposure": cvss3})
    vuln_data["Affected IP Address"].append(ip)

print(new_dict)

Output

{
'202845': {'Vulnerability ID': '202845', 'Name': 'dsm1', 'Affected IP Address': ['10.0.7.2', '10.0.8.1'], 'Risk Exposure': '5.30000019'}, 
'149055': {'Vulnerability ID': '149055', 'Name': 'bitbucket', 'Affected IP Address': ['10.0.6.4', '10.0.5.9'], 'Risk Exposure': '7.5'}, 
'201637': {'Vulnerability ID': '201637', 'Name': 'logstash1', 'Affected IP Address': ['10.0.9.12', '10.0.7.24', '10.0.8.5'], 'Risk Exposure': '7.80000019'}, 
'151610': {'Vulnerability ID': '151610', 'Name': 'bitbucket2', 'Affected IP Address': ['10.0.6.7', '10.0.5.4'], 'Risk Exposure': '5.30000019'}
    }

When I ran

vuln_data = new_dict.setdefault(vuln_id, {"Vulnerability ID": vuln_id,
"Name": host, "Affected IP Address": [], "Risk Exposure": cvss3})

It obviously sets the key to be vuln_id which I would prefer not to, I want the key value pairs, I just don’t want to have the vuln_id in front as well. This output below is what I really need:

main.py (part 2)

new_list = new_dict.values()
print(new_list)

Output

[
{'Vulnerability ID': '202845', 'Name': 'dsm1', 'Affected IP Address': ['10.0.7.2', '10.0.8.1'], 'Risk Exposure': '5.30000019'},
{'Vulnerability ID': '149055', 'Name': 'bitbucket', 'Affected IP Address': ['10.0.6.4', '10.0.5.9'], 'Risk Exposure': '7.5'}, '201637': 
{'Vulnerability ID': '201637', 'Name': 'logstash1', 'Affected IP Address': ['10.0.9.12', '10.0.7.24', '10.0.8.5'], 'Risk Exposure': '7.80000019'},
{'Vulnerability ID': '151610', 'Name': 'bitbucket2', 'Affected IP Address': ['10.0.6.7', '10.0.5.4'], 'Risk Exposure': '5.30000019'}
]

Now I have a list of these dictionaries and this is all the relevant info that I need, but I’m running into an error when trying to write all of this to a CSV file like-so:

main.py (part 3)

keys = new_list[0].keys()

with open('output.csv', 'w', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(new_list)

Error

line 16, in <module>
    keys = new_list[0].keys()
TypeError: 'dict_values' object is not subscriptable

I’m really not sure how to get this to work correctly- but with I just want to write to a cdv file with the keys as the column names/header (ie. Vulnerability ID, Name, etc.) and the values to get written as a row

Asked By: Mitchell Privett

||

Answers:

I’m just mentioning my way to do it.

l = [
{'Vulnerability ID': '202845', 'Name': 'dsm1', 'Affected IP Address': ['10.0.7.2', '10.0.8.1'], 'Risk Exposure': '5.30000019'},
{'Vulnerability ID': '149055', 'Name': 'bitbucket', 'Affected IP Address': ['10.0.6.4', '10.0.5.9'], 'Risk Exposure': '7.5'},
{'Vulnerability ID': '201637', 'Name': 'logstash1', 'Affected IP Address': ['10.0.9.12', '10.0.7.24', '10.0.8.5'], 'Risk Exposure': '7.80000019'},
{'Vulnerability ID': '151610', 'Name': 'bitbucket2', 'Affected IP Address': ['10.0.6.7', '10.0.5.4'], 'Risk Exposure': '5.30000019'}
]

import pandas as pd
df=pd.DataFrame(l)
print(df)
df.to_csv("outputs.csv", index=False)

output column names as keys

enter image description here

Answered By: Bhargav
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.