saving object array to ascii file
Hello Everybody, Sorry if it's a trivial question. I'm trying to find out if there is a way to save object array to ascii file. numpy.savetxt() in NumPy V1.3.0 doesn't seem to work:
import numpy as np obj_arr = np.zeros((2,), dtype=np.object) obj_arr[0] = np.array([[1,2,3], [4,5,6], [7,8,9]]) obj_arr[1] = np.array([[10,11], [12,13]]) obj_arr array([[[1 2 3] [4 5 6] [7 8 9]], [[10 11] [12 13]]], dtype=object)
np.savetxt('obj_array.dat', obj_arr) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy/lib/io.py", line 636, in savetxt fh.write(format % tuple(row) + '\n') TypeError: float argument required
scipy.io.savemat() supports Matlab format of the object array, but I could not find any documentation on ASCII file format for object arrays. Thanks in advance, Masha -------------------- liukis@usc.edu
2010/5/7 Maria Liukis <liukis@usc.edu>:
Sorry if it's a trivial question. I'm trying to find out if there is a way to save object array to ascii file. numpy.savetxt() in NumPy V1.3.0 doesn't seem to work:
Maybe according to http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html#numpy... the *fmt* argument can help here:
numpy.savetxt('obj_array.dat', obj_arr, fmt = '%s')
This uses Python str() to convert the elements into Python strings. You may also use '%r' which uses repr() instead. I think this is the only way because noone except the objects themselves know how to be printed. Friedrich
participants (2)
-
Friedrich Romstedt
-
Maria Liukis