Hristo Mihaylov
Hristo Mihaylov

Reputation: 23

Multiple CSV Python sort

Thanks for the help so far! I got up to this point in the code.

import glob
import csv
import sys
import array

x=1
signs=array.array('i',(0,)*31) 
files = glob.glob('./*.csv')
file = open("all.csv", 'w') 

for row in files: 
    if x<30: 
        signs[x]= x 
        x=x+1 

print (signs[x])
file.close()

I have problems with printing the whole array. As I said, There are 29 files with 30 values each in the model :
1,2
2,9
3,20
4,6
5,2
There is no particular order or anything and numbers do repeat themselves. I need to print out the numbers and how many of them have actually repeated themselves. I seem to create the file all.csv but it appears to be empty. I am really new to python please don't rage at me. I searched for about 8 hours now (including previous code block which I deleted) but I appear to be stuck.

Upvotes: 1

Views: 296

Answers (1)

juniper-
juniper-

Reputation: 6582

One option: iterate over each number in each file and increment a counter that is stored in a dictionary. Print out the results sorted by the dictionary keys (which are the numbers encountered in the csv files).

import csv, glob, sys

from collections import defaultdict

files = [open(f) for f in glob.glob('user./[1-29].csv')]
#files = [open('input1','r')]
counts = defaultdict(int)

for f in files:
    r = csv.reader(f)
    for line in r:
        for num in line:
            counts[int(num)] += 1

for key,val in sorted(counts.items()):
    print key, val

Upvotes: 1

Related Questions