Home > database >  How to store np.ndarray to file?
How to store np.ndarray to file?

Time:02-24

I have a ndarray with the shape (15,11,5). How to save the array to 5 files that includes 11 columns and 15 rows?

import numpy as np

a = np.ndarray(shape=(15,11,5), dtype=float, order='F')

for i in range(5):
    with open('file{i}.txt', 'wb') as f:
        np.savetxt(f, np.column_stack(a[:,:,i]), fmt='%1.10f')

CodePudding user response:

import numpy as np

a = np.ndarray(shape=(11,15,11), dtype=float, order='F')

k = 0
for j in a:
    k =1
    with open(f'file{k}.txt', 'wb') as f:
        np.savetxt(f, j, fmt='%1.10f', delimiter=',')

Try this ^

  • Related