On Tue, 14 Feb 2012 22:55:52 +0100, Daniele Zambelli wrote:
Sto lavorando con numpy, ho creato una struttura che contiene alcunidati, ma quando tento di salvare l'array usando la funzione np.savetxtottengo un errore per me poco comprensibile. Riporto un pezzo di codice e l'errore:irt = [('x', float), ('y', float), ('size', float)] dt = np.dtype([('time', int), ('acc', [('x', float), ('y', float), ('z', float)]), ('ir', [('ir0', irt), ('ir1', irt), ('ir2', irt), ('ir3', irt)]), ]) data = [(0, (1, 2, 3), ((10, 20, 30),(10, 20, 30),(10, 20, 30),(10, 20, 30))), (1, (3, 4, 5), ((30, 40, 50),(30, 40, 50),(30, 40, 50),(30, 40, 50))), (2, (5, 6, 7), ((50, 60, 70),(50, 60, 70),(50, 60, 70),(50, 60, 70))), (3, (7, 8, 9), ((70, 80, 90),(70, 80, 90),(70, 80, 90),(70, 80, 90)))] arraydata = np.array(data, dtype=dt) np.savetxt('testa.txt', arraydata) Traceback (most recent call last): File ".../dtype.py", line 118, in <module> np.savetxt('testa.txt', arraydata) File "/usr/lib/pymodules/python2.7/numpy/lib/npyio.py", line 886, in savetxt fh.write(asbytes(format % tuple(row) + newline)) TypeError: float argument required, not numpy.void Qualcuno saprebbe darmi qualche indicazione?
Quello che capisco io è che numpy.void è il tipo degli oggetti che hai messo in arraydata
In [14]: type(arraydata[0]) Out[14]: <type 'numpy.void'> L'help di savetxt dice? ...Definition: numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n')
Docstring: Save an array to a text file. Parameters ---------- ... fmt : str or sequence of strs A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. Il tuo errore dice > fh.write(asbytes(format % tuple(row) + newline)) > TypeError: float argument required, not numpy.voidne capisco che format è un segnaposto che vuole un float, tipo %f, e questi void gli stanno indigesti.
Infatti usando: In [20]: np.savetxt('testa.txt', arraydata, fmt="%s") un file viene salvato (non so se contiene quello che ti aspettavi).Altro non so: non ho mai usato numpy con tipi che non fossero quelli di base.
-- Daniele Varrazzo - Develer S.r.l. http://www.develer.com _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python