2

I would like to save arrays in loop to one file, so instead of,

x = np.array([1,2,3,4,5])
y = np.array([7,6,5,2,1])
np.savetxt('out.txt', np.array([x,y]))

I would like to be able to add arrays in loop:

for i in range(0,2):
    x[i] = np.array([1,2,3,8,3])
    np.savetxt('out.txt', x[i])

and do not overwrite previous arrays. Is there anything else except savetxt I could use?

2 Answers 2

1

If you want to save as plain text you can do:

with open('out.txt', 'a') as f:
    for i in range(0,2):
        x[i] = np.array([1,2,3,8,3])
        np.savetxt(f, x[i])

Note the 'a' for 'append'.

Sign up to request clarification or add additional context in comments.

Comments

1

np.savez() is made just for this: http://docs.scipy.org/doc/numpy/reference/generated/numpy.savez.html#numpy.savez

Not only will it let you save multiple arrays in one file, even if they have different columns etc., it will be more efficient and compact.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.