Skip to content
Related Articles

Related Articles

How to save a NumPy array to a text file?
  • Last Updated : 29 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

Let us see how to save a numpy array to a text file.

Method 1: Using File handling
Crating 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:




    # 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:




    # 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 the this approach:



  • Example 1:




    # 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:




    # 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.]]
    

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :