Home > Article > Backend Development > How to Save NumPy Arrays as Human-Readable CSV Files?
Saving NumPy Arrays as Human-Readable CSV Files
NumPy offers a convenient way to export data into a human-readable format suitable for various use cases. To achieve this, we leverage the numpy.savetxt function.
To dump a 2D NumPy array into a CSV file, follow these steps:
Example:
Consider the following 2D NumPy array:
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
To export this array to a CSV file named "foo.csv" with a comma separator, use the following code:
import numpy numpy.savetxt("foo.csv", a, delimiter=",")
This will create a CSV file containing the array's values in a human-readable format:
1,2,3 4,5,6 7,8,9
The above is the detailed content of How to Save NumPy Arrays as Human-Readable CSV Files?. For more information, please follow other related articles on the PHP Chinese website!