If you need to save multidimensional arrays in a text file, you can use the header parameter to save the original form of the array:
import numpy as np a = np.random.random((2, 3, 4, 5)) header = ','.join(map(str, a.shape)) np.savetxt('test.txt', a.reshape(-1, a.shape[-1]), header=header, delimiter=',')
And to load this array you can:
with open('test.txt') as f: shape = map(int, f.next()[1:].split(',')) b = np.genfromtxt(f, delimiter=',').reshape(shape)
source share