numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)
: This method is used to save an array to a text file.
Parameters:
fname : If the filename ends in .gz
, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
X : [1D or 2D array_like] Data to be saved to a text file.
fmt : 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.
delimiter : String or character separating columns.
newline : String or character separating lines.
header : String that will be written at the beginning of the file.
footer : String that will be written at the end of the file.
comments : 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 : Encoding used to encode the output file. 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’.
Code #1:
import numpy as geek
x = geek.arange( 0 , 10 , 1 )
print ( "x is:" )
print (x)
c = geek.savetxt( 'geekfile.txt' , x, delimiter = ', ' )
a = open ( "geekfile.txt" , 'r' )
print ( "the file contains:" )
print (a.read())
|
Output :
x is:
[0 1 2 3 4 5 6 7 8 9]
the file contains:
0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
6.000000000000000000e+00
7.000000000000000000e+00
8.000000000000000000e+00
9.000000000000000000e+00
Code #2:
import numpy as geek
x = geek.arange( 0 , 10 , 1 )
y = geek.arange( 10 , 20 , 1 )
z = geek.arange( 20 , 30 , 1 )
print ( "x is:" )
print (x)
print ( "y is:" )
print (y)
print ( "z is:" )
print (z)
c = geek.savetxt( 'geekfile.txt' , (x, y, z))
a = open ( "geekfile.txt" , 'r' )
print ( "the file contains:" )
print (a.read())
|
Output :
x is:
[0 1 2 3 4 5 6 7 8 9]
y is:
[10 11 12 13 14 15 16 17 18 19]
z is:
[20 21 22 23 24 25 26 27 28 29]
the file contains:
0.000000000000000000e+00 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 7.000000000000000000e+00 8.000000000000000000e+00 9.000000000000000000e+00
1.000000000000000000e+01 1.100000000000000000e+01 1.200000000000000000e+01 1.300000000000000000e+01 1.400000000000000000e+01 1.500000000000000000e+01 1.600000000000000000e+01 1.700000000000000000e+01 1.800000000000000000e+01 1.900000000000000000e+01
2.000000000000000000e+01 2.100000000000000000e+01 2.200000000000000000e+01 2.300000000000000000e+01 2.400000000000000000e+01 2.500000000000000000e+01 2.600000000000000000e+01 2.700000000000000000e+01 2.800000000000000000e+01 2.900000000000000000e+01
Code #3: TypeError
import numpy as geek
x = geek.arange( 0 , 10 , 1 )
y = geek.arange( 0 , 20 , 1 )
z = geek.arange( 0 , 30 , 1 )
print ( "x is:" )
print (x)
print ( "y is:" )
print (y)
print ( "z is:" )
print (z)
c = geek.savetxt( 'geekfile.txt' , (x, y, z))
|
Output:
fh.write(asbytes(format % tuple(row) + newline))
TypeError: only length-1 arrays can be converted to Python scalars
During handling of the above exception, another exception occurred:
% (str(X.dtype), format))
TypeError: Mismatch between array dtype (‘object’) and format specifier (‘%.18e’)
Note that if the numpy arrays are not of equal dimension error occurs.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Dec, 2018
Like Article
Save Article