[Numpy-discussion] Multiple string formatting while writing an array into a file
Mon Oct 19 11:32:18 CDT 2009
On Sun, Oct 18, 2009 at 12:03 PM, Gökhan Sever <firstname.lastname@example.org>wrote:
> I have a relatively simple question which I couldn't figure out myself yet.
> I have an array that I am writing into a file using the following savetxt
> np.savetxt(fid, output_array, fmt='%12.4f', delimiter='')
> However, I have made some changes on the code and I require to write after
> 7th element of the array as integer instead of 12.4 formatted float. The
> change below doesn't help me to solve the problem since I get a "ValueError:
> setting an array element with a sequence."
> np.savetxt(fid, (output_array[:7], output_array[7:]), fmt=('%12.4f',
> '%12d'), delimiter='')
> What would be the right approach to fix this issue?
Pre-defining a format like shown below, seemingly help me to fix:
I: for i in range(len(output_array)):
format += "%12.4f "
format += "%12d "
np.savetxt(fid, output_array, fmt=format)
However couldn't figure out to make it work in-place. From the
*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
Any ideas how to make this work via in-place iteration? I could add an
example to the function doc once I learn how to do this.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion