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

Alan Gauld alan.gauld at btinternet.com
Thu Oct 14 10:57:57 CEST 2010

"Hanlie Pretorius" <hanlie.pretorius at gmail.com> wrote

> # 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])))
> Instead, I get an error
> TypeError: float argument required, not numpy.ndarray

Try using %s instead of %f just to see what gets printed
(%s should print just about anything you throw at it...)

Also you don;t really need string formatting here it
would probably be clearer to just do:

writer.writerow( ( file_details[0], file_details[1], str(data[0])  ) )

> 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

No idea and since you haven't shown us where data comes
from it would be pure speculation. Thats why I'm suggesting
using str() or %s to see what Python thinks data[0] really is...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list