How to save a NumPy array to a text file?

18 Jun, 2021
Let us see how to save a numpy array to a text file.
Method 1: Using File handling Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function. Below are some programs of the this approach: 
Example 1: 
Python3
# Program to save a NumPy array to a text file # Importing required librariesimport numpy # Creating an arrayList = [1, 2, 3, 4, 5]Array = numpy.array(List) # Displaying the arrayprint('Array:\n', Array)file = open("file1.txt", "w+") # Saving the array in a text filecontent = str(Array)file.write(content)file.close() # Displaying the contents of the text filefile = open("file1.txt", "r")content = file.read() print("\nContent in file1.txt:\n", content)file.close()
Output: 
Array:
 [1 2 3 4 5]

Content in file1.txt:
 [1 2 3 4 5]
Example 2: 
Python3
# Program to save a NumPy array to a text file # Importing required librariesimport numpy # Creating 2D arrayList = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Array = numpy.array(List) # Displaying the arrayprint('Array:\n', Array)file = open("file2.txt", "w+") # Saving the 2D array in a text filecontent = str(Array)file.write(content)file.close() # Displaying the contents of the text filefile = open("file2.txt", "r")content = file.read() print("\nContent in file2.txt:\n", content)file.close()
Output: 
Array:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

Content in file2.txt:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Method 2:
Using NumPy functions
After creating the array, the 
numpy.savetxt()
function can be used to save the array into a text file. Below are some programs of this approach.
Example 1: 
Python3
# Program to save a NumPy array to a text file # Importing required librariesimport numpy # Creating an arrayList = [1, 2, 3, 4, 5]Array = numpy.array(List) # Displaying the arrayprint('Array:\n', Array) # Saving the array in a text filenumpy.savetxt("file1.txt", Array) # Displaying the contents of the text filecontent = numpy.loadtxt('file1.txt')print("\nContent in file1.txt:\n", content)
Output: 
Array:
 [1 2 3 4 5]

Content in file1.txt:
 [1. 2. 3. 4. 5.]
Example 2: 
Python3
# Program to save a NumPy array to a text file # Importing required librariesimport numpy # Creating 2D arrayList = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Array = numpy.array(List) # Displaying the arrayprint('Array:\n', Array) # Saving the 2D array in a text filenumpy.savetxt("file2.txt", Array) # Displaying the contents of the text filecontent = numpy.loadtxt('file2.txt')print("\nContent in file2.txt:\n", content)
Output: 
Array:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]

Content in file2.txt:
 [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]
 
anikakapoor
Python numpy-io
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Introduction To PYTHON
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Create a directory in Python