xxxxxxxxxx
# 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)
xxxxxxxxxx
# We create a rank 1 ndarray
x = np.array([1, 2, 3, 4, 5])
# We save x into the current directory as
np.save('my_array', x)
# We load the saved array from our current directory into variable y
y = np.load('my_array.npy')
xxxxxxxxxx
np.savetxt(fileDir, M, delimiter=" ", fmt="%10.6f")
# where M can be one or more than one array, a matrix, etc.