[Tutor] Writing elements of an array to a file using CSV module

Hanlie Pretorius hanlie.pretorius at gmail.com
Thu Oct 14 10:18:05 CEST 2010


Hi,

I have a numpy array ('data') that is the result of reading a netCDF
file, and it typically looks like this:

array([ 0.,  0.,  0.,  0.], dtype=float32)


I want to write this, after a date and time, to a CSV file, so the CSV
file would have the entry:

    2000-02-01,09:00,0.0,0.0,0.0,0.0


The code I'm using to test it, is:

# file_details[0] is the date and file_details[1] is the time
writer.writerow(('%s' % (file_details[0]),'%s' %
(file_details[1]),'%f' % (data[0])))


In my opinion, this should give an output line:

2000-02-01,09:00,0.0


Instead, I get an error

TypeError: float argument required, not numpy.ndarray



Can someone please explain to me why data[0] is giving a numpy array
rather than one element? I don't understand it, since I get:

>>> data[0]
0.0


Thanks
Hanlie


More information about the Tutor mailing list