In this post we will be creating a Python script that will transfer the contents of a Numpy array to a text file. To do this we will be using the Numpy ‘savetxt()’ function which will save an existing array to a text file.
See below where we create our Numpy array for this example. Our array contains three columns with three rows of data.
import numpy
Array = numpy.asarray([ [1,2,3,4,5],
[101,102,103,104,105],
[661,662,663,664,665] ])
To transfer our array above to a text file we would use the snippet of code below. Adding a comma as a delimiter will make it easier to transfer this data back in to Numpy if ever required.
numpy.savetxt("numpy.txt", Array, delimiter=",")
The code in full for this project can be seen below.
import numpy
Array = numpy.asarray([ [1,2,3,4,5],
[101,102,103,104,105],
[661,662,663,664,665] ])
numpy.savetxt("numpy.txt", Array, delimiter=",")
Take a look at some of our other content around the Python programming language by clicking here.