numpy.savetxt() Last Updated : 13 Dec, 2018 Comments Improve Suggest changes 1 Likes Like Report 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: Python 1== # Python program explaining # savetxt() function import numpy as geek x = geek.arange(0, 10, 1) print("x is:") print(x) # X is an array c = geek.savetxt('geekfile.txt', x, delimiter =', ') a = open("geekfile.txt", 'r')# open file in read mode 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: Python 1== # Python program explaining # savetxt() function 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) # x, y, z are 3 numpy arrays with same dimension c = geek.savetxt('geekfile.txt', (x, y, z)) a = open("geekfile.txt", 'r')# open file in read mode 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 Python 1== # Python program explaining # savetxt() function 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) # x, y, z are 3 numpy arrays without having same dimension 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. Comment A ArkadipGhosh Follow 1 Improve A ArkadipGhosh Follow 1 Improve Article Tags : Python Python-numpy Python numpy-io Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like