Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Writing Counter to a csv file

I have a csv file of data that has the columns ‘number’, ’colour’, ’number2’, ’foo’, ’bar’, which looks like:

12, red, 124, a, 15p
14, blue, 353, c, 7g
12, blue, 125, d, 65h
12, red, 124, c, 12d

I want to count the number of times number, colour and number2 occur together, so for example, the output from the above list would be: ’12, red, 124 :2’,’14, blue, 353: 1’, ’12, blue, 125: 1’. I’ve done this by using:

import csv
datafile=open('myfile.csv','r')
usefuldata=[] 
for line in datafile: 
    usefuldata.append(line) 
from collections import Counter
outfile1=Counter((line[1],line[2],line[3]) for line in usefuldata)  
print(outfile1)

This gives me :

Counter({(‘12’,’red’,’135’): 21, (‘15’,’blue’,’152’):18, (‘34’,’green’,’123’):16 etc})

Which is great, but I’d like to write this out to a file. I'd like the file to have 4 columns: number, colour, number2, and count. I realise this is a common question and I’ve tried a few different approaches suggested on other threads, but none have worked.

Newfile=open(‘newfile.csv’,’wb’)
fieldnames=['a','b']
csvwriter=csv.DictWriter(newfile, delimiter=',', fieldnames=fieldnames)
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
for row in outfile1:
    csvwriter.writerow(row)

And

with open('newfile.csv','wb') as csvfile:
    fieldnames=['number','colour','number2']
    writer=csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow(Counter((line[1],line[2],line[3]) for line in usefuldata))
    countwriter=csv.writer(csvfile, delimiter=', ')
    countwriter.writerow(outfile1)

Both give me the error

    return self.writer.writerow(self._dict_to_list(rowdict))
TypeError: 'str' does not support the buffer interface

I've also tried using pickle:

import pickle
with open('newfile.csv','wb') as outputfile:
    pickle.dump(outfile1, outputfile)

gives me gibberish files.

My current attempt is to use

writer=csv.DictWriter(newfile, outfile1)
for line in outfile1:
    writer.writerow(line)

but this gives me an error about fieldnames.

I know this is a common question and I'm conscious that I'm only struggling because I really don't know what I'm doing- it has been a few years since I've used python and I've forgotten so much. Any help would be greatly appreciated.

like image 753
Snaaa Avatar asked Sep 16 '15 15:09

Snaaa


2 Answers

First of all, the reason for the main issue -

TypeError: 'str' does not support the buffer interface

is that you are openning the file in binary mode, you should open the file in text mode ( without b ).

Secondly, I would say it would be easier to use normal csv.writer than csv.DictWriter() in your case, because of the way your dictionary is created.

A way to write your result to csv would be -

#Assuming you have previously created the counter you want to write
#lets say you stored the counter in a variable called cnter
with open('newfile.csv','w') as csvfile:
    fieldnames=['number','colour','number2','count']
    writer=csv.writer(csvfile)
    writer.writerow(fieldnames)
    for key, value in cnter.items():
        writer.writerow(list(key) + [value]) 
like image 196
Anand S Kumar Avatar answered Sep 20 '22 13:09

Anand S Kumar


import csv

Assuming count is a Python 3 Counter.
If key is a string, to not split it in every character it contains :

with open(root+'counter_test.csv','w') as csvfile:
    writer=csv.writer(csvfile)
    for key, value in count.items():
        writer.writerow([key] + [value])

And even simpler (take care of the 's' to writerows() function) :

with open(root+'counter_test.csv','w') as csvfile:
    writer=csv.writer(csvfile)
    writer.writerows(count.items())
like image 31
JC Garnier Avatar answered Sep 22 '22 13:09

JC Garnier