Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
How to save a NumPy array to a text file?
Next article icon

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
How to save a NumPy array to a text file?

R

riturajsaha
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-io
Practice Tags :
  • python

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: Python3 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 whic
    2 min read
    How to Convert an image to NumPy array and saveit to CSV file using Python?
    Let's see how to Convert an image to NumPy array and then save that array into CSV file in Python? First, we will learn about how to convert an image to a numpy ndarray. There are many methods to convert an image to ndarray, few of them are: Method 1: Using PIL and NumPy library. We will use PIL.Ima
    4 min read
    How to Save Seaborn Plot to a File in Python?
    Seaborn provides a way to store the final output in different desired file formats like .png, .pdf, .tiff, .eps, etc. Let us see how to save the output graph to a specific file format. Saving a Seaborn Plot to a File in Python Import the inbuilt penguins dataset from seaborn package using the inbuil
    2 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
    3 min read
    How to convert NumPy array to list ?
    This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences