vendredi 8 mai 2015

python 2.7 divide list produced from counter into separate columns in csv

I have two columns of data from a csv file that I've counted (basically the number of tags per chapter in a novel). I now need to separate this data into three columns: tag, chapter number, and count. Here's what I have so far:

with open('test.csv', 'r') as z:
reader = csv.reader(z)
for row in reader:
    labels.append(row[0])
    chapLabels.append(int(row[1]))
    combo = zip(labels, chapLabels)
    c = collections.Counter(combo) 
    d = c.items()

    with open('output.csv', 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=",")
        writer.writerows(d)

This outputs two columns, the first is a list of my counts that includes a string and an integer in this format: ("tag1", 37); the second is the chapter number. I'm still a beginner but have searched widely for an answer to this question and have come up short.

Aucun commentaire:

Enregistrer un commentaire