Save an array to a text file.
If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
Data to be saved to a text file.
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. For complex X, the legal options for fmt are:
fmt='%.4e', resulting in numbers formatted like ' (%s+%sj)' % (fmt, fmt)
' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej' for 3 columns['%.3e + %.3ej', '(%.15e%+.15ej)'] for 2 columnsString or character separating columns.
String or character separating lines.
String that will be written at the beginning of the file.
String that will be written at the end of the file.
String that will be prepended to the header and footer strings, to mark them as comments. Default: ‘# ‘, as expected by e.g. numpy.loadtxt.
Encoding used to encode the outputfile. Does not apply to output streams. If the encoding is something other than ‘bytes’ or ‘latin1’ you will not be able to load the file in NumPy versions < 1.14. Default is ‘latin1’.
See also
saveSave an array to a binary file in NumPy .npy format
savezSave several arrays into an uncompressed .npz archive
savez_compressedSave several arrays into a compressed .npz archive
Further explanation of the fmt parameter (%[flag]width[.precision]specifier):
- : left justify
+ : Forces to precede result with + or -.
0 : Left pad the number with zeros instead of space (see width).
Minimum number of characters to be printed. The value is not truncated if it has more characters.
d,i,o,x), the minimum number of digits.e, E and f specifiers, the number of digits to print after the decimal point.g and G, the maximum number of significant digits.s, the maximum number of characters.c : character
d or i : signed decimal integer
e or E : scientific notation with e or E.
f : decimal floating point
g,G : use the shorter of e,E or f
o : signed octal
s : string of characters
u : unsigned decimal integer
x,X : unsigned hexadecimal integer
This explanation of fmt is not complete, for an exhaustive specification see [1].
Format Specification Mini-Language, Python Documentation.
>>> import numpy as np
>>> x = y = z = np.arange(0.0,5.0,1.0)
>>> np.savetxt('test.out', x, delimiter=',') # X is an array
>>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
>>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/generated/numpy.savetxt.html