[SciPy-Dev] np.savetxt: apply patch in enhancement ticket 1079 to add headers?

Skipper Seabold jsseabold@gmail....
Tue Jun 1 12:48:28 CDT 2010

On Tue, Jun 1, 2010 at 1:05 PM, Stefan <stefan.czesla@hs.uni-hamburg.de> wrote:
> Skipper Seabold <jsseabold <at> gmail.com> writes:
>> Hi all,
>> I am assuming that this is ok to request via the list...  Could we
>> discuss or could someone apply the patch in enhancement ticket 1079?
>> http://projects.scipy.org/numpy/ticket/1079
>> I needed this functionality recently, and this is a quick and easy fix
>> that may have been overlooked.
>> There is also another enhancement request about this here:
>> http://projects.scipy.org/numpy/ticket/1236
>> The only thing that I can think of that might need to be added is a
>> test to see that the header length is the same as the number of
>> columns, but really that might just be up to the user to supply the
>> right headers.  It might also be nice to have a header = True, that
>> uses the field names for a structured array, but I can live without
>> that.
>> Cheers,
>> Skipper
> Hi,

And here I was thinking no one was listening so long ago.

> +1; we have the same problem quite frequently. Our current solution looks
> similar to what has been proposed in ticket 1079, and we wonder why a solution
> has not yet found its way into the official release of numpy.
> We can, however, image a slightly different implementation and would like to
> hear the community's opinion on it.
> If the header is given as a plane string (such as envisaged in ticket 1079), the
> user has to care for the correct formatting, in particular, the user has to
> supply the comment character(s) and the new line formatting. This might be
> against intuition, because many users will at first try to supply their
> header(s) without specifying those formatting characters. The result will be a
> file not readable with numpy.loadtxt, and the error might not be detected right
> away.

I'm not sure I understand why I would want to specify a comment
character for writing a csv file (unless of course I had some comments
to add).

Also note that since that patch was written, savetxt takes a user
supplied newline keyword, so you can just append that to the header

> As numpy.loadtxt has a default comment character ('#'), the same may be
> implemented for numpy.savetxt. In this case, numpy.savetxt would get two
> additional keywords (e.g. header, comment(character)), which bloats the
> interface, but potentially provides more safety.

FWIW, I ended up rolling my own using the most recent pre-Python 3
changes for savetxt that accepts a list of names instead of one string
or if the provided array has the attribute dtype.names (non-nested rec
or structured arrays) it uses those.  Whatever is done I think the
support for structured arrays is nice, and I think having this
functionality is a no-brainer.  I need it quite often.


More information about the SciPy-Dev mailing list