0

So, I wrote small test code for calculating the averages from all the csv files in a folder.

parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()

# Get command arguments
input_files_path = args.input
output = args.output

avg = []
for infile in glob.glob(input_files_path+"*.csv"):
        a = np.loadtxt(infile, delimiter=",")
        mean = np.mean(a, axis=0)
        avg.append((mean))
        print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")

but the issue now, when I am trying to save the name of the file as well in the csv file like this:

parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()

# Get command arguments
input_files_path = args.input
output = args.output

avg = []
for infile in glob.glob(input_files_path+"*.csv"):
        a = np.loadtxt(infile, delimiter=",")
        mean = np.mean(a, axis=0)
        avg.append((infile,(mean)))
        print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")

it throws an error:

    np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
  File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 963, in savetxt
    X = np.asarray(X)
  File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 235, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: cannot set an array element with a sequence

I don't seem to understand what the issue is. Maybe it is because I am trying to append a string to a float? I check the fmts in numpy.. but I did not find anything interesting.

/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py:773: UserWarning: loadtxt: Empty input file: "../robalo-31.1/op.csv"
  warnings.warn('loadtxt: Empty input file: "%s"' % fname)
/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:2374: RuntimeWarning: invalid value encountered in double_scalars
  return mean(axis, dtype, out)
4
  • I don't see the line avg.append((infile,(mean))) anywhere in your code. Could you please clarify how that fits into this? Commented Jan 31, 2013 at 21:11
  • Posted. Is there something else that I am missing? Added after the edit @mgilson Commented Jan 31, 2013 at 21:15
  • Thanks for the update. I'm guessing that it has something to do with the format portion in savetxt -- Basically you're telling numpy to save everything as a float, but you've got strings in your data. I can imagine that could mess up the dtype argument when numpy tries to convert the list to an ndarray. Rather than using np.savetxt, could I recommend that you look into the excellent csv module for saving the output file? Commented Jan 31, 2013 at 21:20
  • Well, do you see any other way of knowing which row of the csv file contains which data? CSV is surely an option. do you suggest any function in that? Commented Jan 31, 2013 at 21:22

1 Answer 1

1

I'm guessing there's a problem with the format characters you're providing with np.savetxt. (you have a string-float tuple and you're delimiter implies you're only using floats). I would suggest csv for this instead:

>>> import csv
>>> import StringIO
>>> g = StringIO.StringIO()
>>> writer = csv.writer(g,delimiter=',')  #this is the part that is applicable
>>> writer.writerows(lst)
>>> g.seek(0)
>>> print g.read()
foo,2
bar,4

So I'm guessing your code could be simply:

with open(output,'wb') as fout:
    writer = csv.writer(fout,delimiter=',') 
    writer.writerows(avg)

instead of

np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
9
  • That would still not do the job. there is some other problem. AttributeError: 'module' object has no attribute 'writer' Commented Jan 31, 2013 at 21:39
  • @user2015933 -- It's definitely in there ... Commented Jan 31, 2013 at 21:42
  • writer = csv.writer(fout,delimiter=', ') AttributeError: 'module' object has no attribute 'writer' fyi, I imported csv. Commented Jan 31, 2013 at 21:43
  • what do you get if you just do a simple from csv import writer outside of your script? Commented Jan 31, 2013 at 21:46
  • 1
    There are a couple reasons why this might be happening. The first reason is that you accidentally set csv = some_other_module in your script -- Although that's unlikely. The second reason it could happen is because you could have a csv.py file somewhere else on your pythonpath (likely the same directory as your script). print csv.__file__ to see where it was imported from. Commented Jan 31, 2013 at 21:55

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.