Hello, I’ve been away from the stdlib project for a long time, and could not find discussions over some features of savetxt that I find useful, following mostly numpy savetxt behavior.
I’ve opened an issue in github do discuss the idea. Looking forward to your opinions!
Proposal
I’d like to propose a change to the interface of savetxt to add some optional arguments that would make it more flexible, following quite closely numpy’s function. Please let me know if there is any interest. As I said I could not find if it already was discussed and discarded.
I am proposing to add several arguments (see proposed signature below)
- Adding the possibility of providing a
unitnumber in place of afilenamegives flexibility to add partial tables to a file or output to stdout. One of them must always be present. fmtGives the possibility of customize the output format. In many cases complex calculations give only a few significant figures and I would like the number saved to express that. Should work in a similar way as it does inloadtxt().headerandfootergive the possibility to add information to the data files (column names, information on parameters used in calculations or measurements, etc). I find this very important because it allows to keep information together with data.- Following numpy I find extremely useful if non-data information is commented or signaled in some form. This would be the use of the argument
comments. - I would propose to change the length of delimiter from len=1 to arbitrary length. For intstance, we may want a *.cvs file to be separated by a comma and one or more spaces ', '.
Some of these changes will probably trigger changes in loadtxt in order to keep compatibility (argument comments).
Also, I am not sure if a “bad-written” data file, with some text instead of numbers breaks the current version of loadtxt.
The spec would be something like:
savetxt - save a 2D array into a text file
Status
Experimental
Description
Saves a rank-2 array into a text file.
Syntax
call [[stdlib_io(module):savetxt(interface)]] (array [, filename | unit] [, delimiter] [, fmt] [, header] [, footer] [, comments])
Arguments
array: Shall be a rank-2 array of type real, complex or integer.
filename (optional): Shall be a character expression containing the name of the file , that will contain the 2D array. Present at the same time than unit will give an error.
unit (optional): Shall be an integer containing the unit of an already opened file. Present at the same time than filename will give an error.
delimiter (optional): Shall be a character expression of any length that contains the delimiter used to separate the columns. The default is a single space ' '.
fmt (optional): Fortran format specifier for the text save. Defaults to the write format for the data type.
header (optional): Shall be a character expression that will be written at the beginning of the file.
footer (optional): Shall be a character expression that will be written at the end of the file.
comments (optional): Shall be a character expression of (length 1 that will be prepended to the header and footer strings to mark them as comments. Default: # .