Open In App

How to save a NumPy array to a text file?

Last Updated : 18 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 libraries
import numpy
 
# Creating an array
List = [1, 2, 3, 4, 5]
Array = numpy.array(List)
 
# Displaying the array
print('Array:\n', Array)
file = open("file1.txt", "w+")
 
# Saving the array in a text file
content = str(Array)
file.write(content)
file.close()
 
# Displaying the contents of the text file
file = 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 libraries
import numpy
 
# Creating 2D array
List = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Array = numpy.array(List)
 
# Displaying the array
print('Array:\n', Array)
file = open("file2.txt", "w+")
 
# Saving the 2D array in a text file
content = str(Array)
file.write(content)
file.close()
 
# Displaying the contents of the text file
file = 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 libraries
import numpy
 
# Creating an array
List = [1, 2, 3, 4, 5]
Array = numpy.array(List)
 
# Displaying the array
print('Array:\n', Array)
 
# Saving the array in a text file
numpy.savetxt("file1.txt", Array)
 
# Displaying the contents of the text file
content = 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 libraries
import numpy
 
# Creating 2D array
List = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Array = numpy.array(List)
 
# Displaying the array
print('Array:\n', Array)
 
# Saving the 2D array in a text file
numpy.savetxt("file2.txt", Array)
 
# Displaying the contents of the text file
content = 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.]]

 



Previous Article
Next Article

Similar Reads

NumPy save() Method | Save Array to a File
The NumPy save() method is used to store the input array in a binary file with the 'npy extension' (.npy). Example: C/C++ Code import numpy as np a = np.arange(5) np.save('array_file', a) SyntaxSyntax: numpy.save(file, arr, allow_pickle=True, fix_imports=True) Parameters: file: File or filename to which the data is saved. If the file is a string or
2 min read
How to extract paragraph from a website and save it as a text file?
Perquisites: Beautiful soupUrllib Scraping is an essential technique which helps us to retrieve useful data from a URL or a html file that can be used in another manner. The given article shows how to extract paragraph from a URL and save it as a text file. Modules Needed bs4: Beautiful Soup(bs4) is a Python library used for getting data from HTML
2 min read
Ways To Save Python Terminal Output To A Text File
Python provides various methods for redirecting and saving the output generated in the terminal to a text file. This functionality can be useful for logging, debugging, or simply capturing the results of a script. In this article, we'll explore different ways to save Python terminal output to a text file. Save Python Terminal Output to a Text File
2 min read
How to save file with file name from user using Python?
Prerequisites: File Handling in PythonReading and Writing to text files in Python Saving a file with the user's custom name can be achieved using python file handling concepts. Python provides inbuilt functions for working with files. The file can be saved with the user preferred name by creating a new file, renaming the existing file, making a cop
5 min read
Save Plot To Numpy Array using Matplotlib
Saving a plot to a NumPy array in Python is a technique that bridges data visualization with array manipulation allowing for the direct storage of graphical plots as array representations, facilitating further computational analyses or modifications within a Python environment. Let's learn how to Save Plot to NumPy Array using Matplotlib. How to Sa
4 min read
How To Save Multiple Numpy Arrays
NumPy is a powerful Python framework for numerical computing that supports massive, multi-dimensional arrays and matrices and offers a number of mathematical functions for modifying the arrays. It is an essential store for Python activities involving scientific computing, data analysis, and machine learning. What is a Numpy array?A NumPy array is a
3 min read
How To Save The Network In XML File Using PyBrain
In this article, we are going to see how to save the network in an XML file using PyBrain in Python. A network consists of several modules. These modules are generally connected with connections. PyBrain provides programmers with the support of neural networks. A network can be interpreted as an acyclic directed graph where each module serves the p
2 min read
Scrape and Save Table Data in CSV file using Selenium in Python
Selenium WebDriver is an open-source API that allows you to interact with a browser in the same way a real user would and its scripts are written in various languages i.e. Python, Java, C#, etc. Here we will be working with python to scrape data from tables on the web and store it as a CSV file. As Google Chrome is the most popular browser, to make
3 min read
Save multiple matplotlib figures in single PDF file using Python
In this article, we will discuss how to save multiple matplotlib figures in a single PDF file using Python. We can use the PdfPages class's savefig() method to save multiple plots in a single pdf. Matplotlib plots can simply be saved as PDF files with the .pdf extension. This saves Matplotlib-generated figures in a single PDF file named Save multip
3 min read
How to save a Python Dictionary to a CSV File?
Prerequisites: Working with csv files in Python CSV (comma-separated values) files are one of the easiest ways to transfer data in form of string especially to any spreadsheet program like Microsoft Excel or Google spreadsheet. In this article, we will see how to save a PYthon dictionary to a CSV file. Follow the below steps for the same. Import cs
2 min read
Practice Tags :
three90RightbarBannerImg