Open In App

How to save a NumPy array to a text file?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with data it's important to know how to save NumPy arrays to text files for storage, sharing and further analysis. There are different ways from manual file handling to using specialized NumPy functions. In this article, we will see how to save a NumPy array to a text file

Method 1: Using File Handling 

This method involves File handling a text file, converting the NumPy array to a string and writing it to the file using the write() function. After saving the array, file is closed using the close() function. Below are examples demonstrating this approach:

Example 1: Saving a 1D Array

  • file = open("file1.txt", "w+"): Opens the file "file1.txt" in write mode and creates the file if it doesn't exist.
  • file.write(content): Writes string representation of array to the file.
  • file.close(): Closes the file after writing to it.
  • file = open("file1.txt", "r"): Opens the file "file1.txt" in read mode to access its contents.
Python
import numpy
List = [1, 2, 3, 4, 5]
Array = numpy.array(List)
print('Array:\n', Array)
file = open("file1.txt", "w+")
content = str(Array)
file.write(content)
file.close()
file = open("file1.txt", "r")
content = file.read()
print("\nContent in file1.txt:\n", content)
file.close()

Output: 

textfile1
1D Array

Example 2: Saving a 2D Array

Python
import numpy
List = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Array = numpy.array(List)
print('Array:\n', Array)
file = open("file2.txt", "w+")
content = str(Array)
file.write(content)
file.close()
file = open("file2.txt", "r")
content = file.read()
print("\nContent in file2.txt:\n", content)
file.close()

Output: 

textfile2
2D Array

Method 2: Using numpy.savetxt()

The numpy.savetxt() function is an efficient way to save NumPy arrays to text files. This method allows us to control formatting such as the number of decimals or delimiter between values. This function saves the data as floating-point values by default even when the original array contains integers.

Example 1: Saving a 1D Array

  • np.savetxt("file1.txt", array, fmt="%d"): Saves 1D array to a text file named file1.txt with the integer format (fmt="%d").
  • content = np.loadtxt('file1.txt', dtype=int): Loads the contents of file1.txt back into a NumPy array as integers (dtype=int).
Python
import numpy as np
array = np.array([1, 2, 3, 4, 5])
print('Array:\n', array)
np.savetxt("file1.txt", array, fmt="%d")
content = np.loadtxt('file1.txt', dtype=int)
print("\nContent in file1.txt:\n", content)

Output: 

textfile3
1D Array

Example 2: Saving a 2D Array with Custom Delimiters

  • np.savetxt("file2.txt", array, delimiter=" ", fmt="%d"): Saves 2D array to a text file with space as the delimiter and integer format (fmt="%d").
  • content = np.loadtxt('file2.txt', delimiter=" ", dtype=int): Loads content from file2.txt back into a NumPy array, interpreting data as integers (dtype=int) with space as the delimiter.
Python
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print('Array:\n', array)
np.savetxt("file2.txt", array, delimiter=" ", fmt="%d")  
content = np.loadtxt('file2.txt', delimiter=" ", dtype=int) 
print("\nContent in file2.txt:\n", content)  

Output: 

textfile4
2D Array

With these simple methods we can easily change our NumPy arrays to a text file as this approach helps us to manage our data more effectively and ensures it’s stored in a readable format.


Next Article
Practice Tags :

Similar Reads