Asa Hunt
Asa Hunt

Reputation: 129

Writing a set plus a formatted string to a CSV?

I'm trying to write the contents of a set and a formatted string to a CSV file using Python 2. I have a set full of domain names, and I need to write the domain name to the first column, skip one column, then write the word 'Related', and repeat on the next row until the set is empty. I've tried a few different things, but this is where I'm at now (broken code).

#!/usr/bin/python

#CSV WRITE TESTING

import csv

testSet = set(['thissucks.in', 'whateverlife.in', 'crapface.in', 'lasttry.in'])
with open('mag.csv', 'ab+') as f:
    writer = csv.writer(f + ' ' + 'Related',dialect= 'excel', delimiter= ',')
    for each in testSet:
        writer.writerow(list(testSet))

Upvotes: 1

Views: 39

Answers (1)

Eric Shaw
Eric Shaw

Reputation: 69

import csv
testSet = set(['thissucks.in', 'whateverlife.in', 'crapface.in', 'lasttry.in'])
with open('mag.csv', 'ab+') as f:
    writer = csv.writer(f ,dialect= 'excel', delimiter= ',')
    for domain in testSet:
        writer.writerow([domain, '', 'Related'] )

Upvotes: 1

Related Questions