bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / savetxt
_ArrayFunctionDispatcher
numpy:savetxt
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_npyio_impl.py :1401
Signature
def savetxt ( fname , X , fmt = %.18e , delimiter = , newline =
, header = '' , footer = '' , comments = # , encoding = None ) Summary
Save an array to a text file.
Parameters
fname: filename, file handle or pathlib.PathIf the filename ends in
.gz, the file is automatically saved in compressed gzip format.loadtxtunderstands gzipped files transparently.X: 1D or 2D array_likeData to be saved to a text file.
fmt: str or sequence of strs, optionalA single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case
delimiteris ignored. For complexX, the legal options forfmtare:a single specifier,
fmt='%.4e', resulting in numbers formatted like' (%s+%sj)' % (fmt, fmt)a full string specifying every real and imaginary part, e.g.
' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'for 3 columnsa list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g.
['%.3e + %.3ej', '(%.15e%+.15ej)']for 2 columns
delimiter: str, optionalString or character separating columns.
newline: str, optionalString or character separating lines.
header: str, optionalString that will be written at the beginning of the file.
footer: str, optionalString that will be written at the end of the file.
comments: str, optionalString that will be prepended to the
headerandfooterstrings, to mark them as comments. Default: '# ', as expected by e.g.numpy.loadtxt.encoding: {None, str}, optionalEncoding 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'.
Notes
Further explanation of the fmt parameter (%[flag]width[.precision]specifier):
flags:
-left justify+Forces to precede result with + or -.0Left pad the number with zeros instead of space (see width).width:
Minimum number of characters to be printed. The value is not truncated if it has more characters.
precision:
For integer specifiers (eg.
d,i,o,x), the minimum number of digits.For
e, Eandfspecifiers, the number of digits to print after the decimal point.For
gandG, the maximum number of significant digits.For
s, the maximum number of characters.
specifiers:
ccharacterdorisigned decimal integereorEscientific notation witheorE.fdecimal floating pointg,Guse the shorter ofe,Eorfosigned octalsstring of charactersuunsigned decimal integerx,Xunsigned hexadecimal integer
This explanation of fmt is not complete, for an exhaustive specification see [1].
Examples
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✓
See also
- save
Save an array to a binary file in NumPy
.npyformat- savez
Save several arrays into an uncompressed
.npzarchive- savez_compressed
Save several arrays into a compressed
.npzarchive
Aliases
-
numpy.savetxt