1

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.

4
  • Could you give a short example of one of the input files please? Are there 30 values in a single row, or 30 rows or something else? Commented Oct 17, 2012 at 11:37
  • 1,2 2,6 3,9 4,17 and so on to 30,something the something is no larger then 29 but could be 29 Commented Oct 17, 2012 at 11:39
  • 3
    This isn't valid Python -- neither i=0: or i++ work. It also looks like you're trying to read from a csv file, "all.csv", which you open for writing (and therefore erase before you read from it.) Commented Oct 17, 2012 at 11:40
  • I dont know what is valid python , I am really new to it and some of the most basic stuff arent avaivable could I write i=i+1 ? Commented Oct 17, 2012 at 11:42

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.