简体   繁体   中英

Write a list of sets to CSV - Python

EDIT: Looks like sets were used to build the dict, and the list was a list of those dicts. @That1Guy provided the correct answer, mentioning that DictWriter solves the problem.

I have a list of sets that contains the following data:

{'dns-a': '93.184.216.34', 'domain-name': 'example.com', 'fuzzer': 'Original*'}
{'dns-a': '54.174.149.30', 'domain-name': 'examplea.com', 'fuzzer': 'Addition'}
{'domain-name': 'exampleb.com', 'fuzzer': 'Addition'}
{'dns-a': '104.130.124.96', 'domain-name': 'axample.com', 'fuzzer': 'Bitsquatting'}
{'domain-name': 'mxample.com', 'fuzzer': 'Bitsquatting'}
{'dns-a': '52.0.22.168', 'domain-name': 'exarnple.com', 'fuzzer': 'Homoglyph'}
{'dns-a': '64.57.183.2', 'domain-name': 'examp1e.com', 'fuzzer': 'Homoglyph'}
{'domain-name': 'exampl-e.com', 'fuzzer': 'Hyphenation'}
{'domain-name': 'exakmple.com', 'fuzzer': 'Insertion'}

How do I write this to a CSV where the first half of the set designates the column name and the second is the value? For example, it should look like this if I open it in Excel:

dns-a          domain-name    fuzzer
93.184.216.34  example.com    Original
54.174.149.30  examplea.com   Addition
               exampleb.com   Addition

You do not have sets , you have dicts . That being the case, you should use the DictWriter class in the csv module.

From the docs:

Create an object which operates like a regular writer but maps dictionaries onto output rows.

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

You can use writerows instead of writerow . Your case:

import csv

data = [{'dns-a': '93.184.216.34', 'domain-name': 'example.com', 'fuzzer': 'Original*'},
        {'dns-a': '54.174.149.30', 'domain-name': 'examplea.com', 'fuzzer': 'Addition'},
        {'domain-name': 'exampleb.com', 'fuzzer': 'Addition'},
        {'dns-a': '104.130.124.96', 'domain-name': 'axample.com', 'fuzzer': 'Bitsquatting'},
        {'domain-name': 'mxample.com', 'fuzzer': 'Bitsquatting'},
        {'dns-a': '52.0.22.168', 'domain-name': 'exarnple.com', 'fuzzer': 'Homoglyph'},
        {'dns-a': '64.57.183.2', 'domain-name': 'examp1e.com', 'fuzzer': 'Homoglyph'},
        {'domain-name': 'exampl-e.com', 'fuzzer': 'Hyphenation'},
        {'domain-name': 'exakmple.com', 'fuzzer': 'Insertion'}]

with open('data.csv', 'w') as data_csv:
    dict_writer = csv.DictWriter(data_csv, data[0].keys())
    dict_writer.writeheader()
    dict_writer.writerows(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM