python - Using savetxt in numpy with custom data types -
i trying export data complex numpy calculation text file can analyze in excel or something. data type of array i'm trying export complicated , defined like:
rowtype = np.dtype([("sn", "s8"), ("freqs", np.uint16, (3,)), ("peaks", np.float32, (3,))])
so each "row" of array 8-character string, 3-element subarray of 16-bit integers, , 3-element subarray of floats. want have 1 row per row in text file, tabs between each element of subarrays. when call savetxt export populated array, supply fmt parameter keep throwing exception?
the format code applies each element in row. because have arrays elements, can control format of array , not individual elements. you'd better off printing array line line, answered this question.
if must use savetxt
, closest request:
np.savetxt(my_file, my_array, ['%s\t', '%s\t', '%s'])
however, if change data type flatten individual arrays, can control formatting of each element. here example csv file:
new_dtype = [('sn', 's8'), ('freqs1', 'i'), ('freqs2', 'i'), ('freqs3', 'i'), ('peaks1', 'f'), ('peaks2', 'f'), ('peaks3', 'f')] np.savetxt(my_file, my_array.astype(new_dtype), '%s,%i,%i,%i,%f,%f,%f')
Comments
Post a Comment