[Numpy-discussion] Multiple string formatting while writing an array into a file

Gökhan Sever gokhansever@gmail....
Mon Oct 19 11:32:18 CDT 2009


On Sun, Oct 18, 2009 at 12:03 PM, Gökhan Sever <gokhansever@gmail.com>wrote:

> Hello,
>
> 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
> method.
>
> 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?
>
> Thanks.
>
> --
> Gökhan
>

Pre-defining a format like shown below, seemingly help me to fix:

I[48]: format=""

I[49]: for i in range(len(output_array)):
    if i<7:
        format += "%12.4f "
    else:
        format += "%12d "

np.savetxt(fid, output_array, fmt=format)


However couldn't figure out to make it work in-place. From the
savetxt<http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html#numpy.savetxt>documentation:

*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.

Thanks.


-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091019/745bb641/attachment.html 


More information about the NumPy-Discussion mailing list