# How To Convert NumPy Files To Text Files In Python

In this article, we will discuss the step-by-step process on how to convert numpy files to text files in Python, we will learn in this in our tutorial with the help of examples.

## How To Save A NumPy Array To A Text File?

We can save the NumPy array file and convert to a text file using the `str()` and file handling function. In this approach, we will convert first the NumPy Array into string using `str()` function and writing it into the text file using the `write()` function. Lastly, we want to close the file using `close()` function.

Here’s the example program below on How To Save A NumPy Array To A Text File:

``````# Program to save a NumPy array to a text file

# import numpy as np import
import numpy

# Creating an array 0
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 file
content = str(Array)
file.write(content)
file.close()

# Displaying the contents of the file
file = open("file1.txt", "r")

print("\nContent in file1.txt:\n", content)
file.close()``````

When you execute the program this will be the output:

``````Array:
[1 2 3 4 5]

Content in file1.txt:
[1 2 3 4 5]``````

Code Explanation:

Here’s the step-by-step explanation in saving a numpy array into a text file.

1. Importing required libraries

``import numpy``

In the line of code above, the required libraries are imported.

2. Creating an array

``````List = [1, 2, 3, 4, 5]
Array = numpy.array(List)``````

On this part of code is for creating an array and store to a variable Array.

3. Displaying the array

``````print('Array:\n', Array)
file = open("file1.txt", "w+")``````

The next line of code given above is for displaying the array using print() function and create a text file using `open()` function and save to a variable file.

4. Saving the array in a text file

``````content = str(Array)
file.write(content)
file.close()``````

Next line of code given above is for saving the array in a text file using `str()` function and store to the variable content, and after that the array will save into the text file using `write()` function.

5. Displaying the contents of the text file

``````file = open("file1.txt", "r")

print("\nContent in file1.txt:\n", content)
file.close()``````

Lastly, the code given above is for displaying the contents of the text file using `read()` function.

## Conclusion

In this tutorial, we have completely discuss the step-by-step process on How To Convert NumPy Files To Text Files In Python using str(), open(), write() and read() functions, we learn it with the help of examples and step-by-step explanations.

I hope you can learn a lot for today’s blog.

To know more about Python Programming Tutorials you can visit this Python Tutorials link and improve your knowledge to become a python developer!