numpy.savetxt#

numpy.savetxt(fname,X,fmt='%.18e',delimiter='',newline='\n',header='',footer='',comments='#',encoding=None)[source]#

Save an array to a text file.

Parameters:
fnamefilename, file handle or pathlib.Path

If the filename ends in.gz, the file is automatically saved incompressed gzip format.loadtxt understands gzipped filestransparently.

X1D or 2D array_like

Data to be saved to a text file.

fmtstr or sequence of strs, optional

A single format (%10.5f), a sequence of formats, or amulti-format string, e.g. ‘Iteration %d – %10.5f’, in whichcasedelimiter is ignored. For complexX, the legal optionsforfmt are:

  • a single specifier,fmt='%.4e', resulting in numbers formattedlike'(%s+%sj)'%(fmt,fmt)

  • a full string specifying every real and imaginary part, e.g.'%.4e%+.4ej%.4e%+.4ej%.4e%+.4ej' for 3 columns

  • a list of specifiers, one per column - in this case, the realand imaginary part must have separate specifiers,e.g.['%.3e+%.3ej','(%.15e%+.15ej)'] for 2 columns

delimiterstr, optional

String or character separating columns.

newlinestr, optional

String or character separating lines.

headerstr, optional

String that will be written at the beginning of the file.

footerstr, optional

String that will be written at the end of the file.

commentsstr, optional

String that will be prepended to theheader andfooter strings,to mark them as comments. Default: ‘# ‘, as expected by e.g.numpy.loadtxt.

encoding{None, str}, optional

Encoding used to encode the outputfile. Does not apply to outputstreams. If the encoding is something other than ‘bytes’ or ‘latin1’you will not be able to load the file in NumPy versions < 1.14. Defaultis ‘latin1’.

See also

save

Save an array to a binary file in NumPy.npy format

savez

Save several arrays into an uncompressed.npz archive

savez_compressed

Save several arrays into a compressed.npz archive

Notes

Further explanation of thefmt parameter(%[flag]width[.precision]specifier):

flags:

- : left justify

+ : Forces to precede result with + or -.

0 : Left pad the number with zeros instead of space (see width).

width:

Minimum number of characters to be printed. The value is not truncatedif it has more characters.

precision:
  • For integer specifiers (eg.d,i,o,x), the minimum number ofdigits.

  • Fore,E andf specifiers, the number of digits to printafter the decimal point.

  • Forg andG, the maximum number of significant digits.

  • Fors, the maximum number of characters.

specifiers:

c : character

d ori : signed decimal integer

e orE : scientific notation withe orE.

f : decimal floating point

g,G : use the shorter ofe,E orf

o : signed octal

s : string of characters

u : unsigned decimal integer

x,X : unsigned hexadecimal integer

This explanation offmt is not complete, for an exhaustivespecification see[1].

References

[1]

Format Specification Mini-Language,Python Documentation.

Examples

>>>importnumpyasnp>>>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
On this page