How to save numpy.ndarray into txt file and read it to a variable

When do you need save a numpy.ndarray into a txt file?

If you want to check value of numpy.ndarray, share your data to others or use other application to process numpy.ndarray data. You may need save numpy.ndarray into a txt file so that you can reused it.

How to save?

We can use numpy.savetxt() function, you must notice that this function only can save numpy.ndarray.

How to read from txt file?

If you get a txt file that saves a numpy.ndarray. How to read this data to a variable so that you can process it by your algorithm. Here you can use numpy.loadtxt() function.

>>> import numpy as np
>>> data = np.arange(0,1,0.1)
>>> np.savetxt("np.txt",data) # save numpy.ndarray to txt file
>>> load_data = np.loadtxt("np.txt") # read numpy.ndarray from txt file
>>> load_data
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])