I have a CSV file containing one column (column 1). I repeat the element in the element and how many times (occcurance_count) .and want to check the print count using the python in the CSV file itself. In the example below
not "241682-27638-American Dalr- OCOF" repeating the count is a "241942-37190-USD Dalr- DIV", the number is 2 and repeated twice and so on.
want to produce as below CSV format
column 1, 1682-27638-US Dalr- OGGCOF, 1 24l,682-27,638 American Dalr- OGGINT, 1 24l,682-27,638 American Dalr- CIGGNT, 1 24l,682-27,638 American Dalr- OCGGINT, 1 24l,942-37,l90 American Dalr- GGDIV, Occcurance_count 2 24l,942-37,l90 American Dalr- CHYOF, 1 24l,942-37,l90 American Dalr- EQPL 1 241942-37190-American Dalr- INT, 1 242,066-l5,343 American Dalr- CYJOF, 3 242,066-l5,343 American Dalr- CYJOF, 3 242066-l5343- US Dalr- CYJOF, 3 242,066-l5,343 American Dalr- ETHQPL , 1 242,066-15,343-USD - INFRT, 1 241,942-37,190-USD- GGDIV 2 242066-33492-American Dalr- CJHOF, 1
I think down The code is what you are looking for. The logic is simple but also long explanation about logic: First you need to open the CSV file to read and list all the elements in the list. Then learn the number of incidence of each list item, open the new CSV file and use the list calculation method to write the item and count for each item.
Of course, there may be an optimization method but this code comes quickly.
excluding imports Try CSV import sys: fr = open ( "mycsv.csv") FW = open ( "mscsv_counter.csv", "w"): print "file Could not open "Reader = CSV Reader (FR) counterlist = list () in the reader for the row: # print row if lane (line) & gt; 0: counterlist.append (row [0]) # For items in counterlist: # print counterlist.count (item) author = csv.writer (fw) data = ["column1", "counter"] writer.writerow ( Data) for counterlist in item: rowdata = [item, counterlist.count (item)] # print rowdata writer.writerow (rowdata) fr.close (); Fw.close ();
Comments
Post a Comment