Saving a 3D numpy array to .txt file Saving a 3D numpy array to .txt file numpy numpy

Saving a 3D numpy array to .txt file


As the docs explain, savetxt can only save 1D and 2D arrays.

And how would you want it to be saved? savetxt saves into a CSV file: there are columns separated by whitespace, and rows separated by newlines, which looks nice and 2D in a text editor. How would you extend that to 3D?

If you have some specific format in mind that you want to use, there may be a formatter for that (or maybe it's just a matter of reshape-ing the array down to 2D?), but you'll have to explain what it is that you want.

If you don't care about editing the output in a text editor, why not just save it in binary format, e.g., numpy.save("nxx.npy", array)? That can handle any shape just fine.


IIRC, old versions of NumPy handled this by flattening all but one of the dimensions down into a single dimension. So, to use your data, you'd have to load it up and then reshape it—which you could only do if you knew, from somewhere else, what the original dimensions were. And you wouldn't even realize the problem until you'd spent all week generating a few TB of files that you now couldn't figure out how to use. (I'd guess this is why they changed the behavior—it was used by accident more often than on purpose, and with annoying consequences.)

Anyway, if you want that behavior, it's of course trivial to rehape the array to 2D yourself and then save that:

np.savetxt("nxx", array.reshape((3,-1)), fmt="%s")

Or, even better:

np.savetxt("nxx", array.reshape((3,-1)), fmt="%s", header=str(array.shape))