I am trying to read data from a CSV file (A), extract data, and write that to a different CSV file (B). In the new file B, I want to have two columns. Column 1 to list names of column 1 in file A and column 2 to list the count of column 1 in file A. So for example, if the file A looks like this without ':' (they are lined up in two columns):
Animal: Gender
Rabbit: Male
Dog: Male
Rabbit: Female
Cat: Male
Cat: Male
Dog: Female
Dog: Male
Turtle: Male
I want the output in file B to look like this (actually in different columns without ':' again):
Animal: Count
Cat: 2
Dog: 3
Rabbit: 2
Turtle: 1
This is the very first time I am doing anything like this and this is what I have so far, but I am failing in having the data printed out in file B and have the "count" done correctly. Could anybody please help me with this?
import csv
ReadData=csv.reader(open('C:\Users\..\FileA.csv','rb'), delimiter=',')
def column(ReadData, i):
return [row[i] for row in ReadData]
for line in ReadData:
WriteData=csv.writer(open('C:\Users\..\FileB.csv','wb'),
delimiter=' ', quotechar=':', quoting=csv.QUOTE_ALL)
print column(ReadData,1)
Thank you for your help in advance!