0

I want to know if numpy has any build-in functions for writing to files or if there is a method, which should be used for writing an array constructed like this:

[[2, 3, 4], [3, 5, 6], [8, 7, 9]]

to a file so it looks like this:

2 3 4 
3 5 6
8 7 9

I am not sure how to this. I do know how to do it with a regular python list using a for loop but I want to know which way this should be done.

1 Answer 1

4

You can just use

np.savetxt( "filename.txt", your_array )

For details see: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.savetxt.html

[Update] You can use the formatting parameter e.g. like this:

your_array = [[2, 3, 4], [3, 5, 6], [8, 7, 9]]
np.savetxt("filename.txt", your_array, fmt="%d")
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know if I should use some additional parameters or how to make it write to file in this manner, but the array in the file I get with this code is in floats. I have an array with only integers like 123, 456,..., but the numbers in the output are 1,2300000, 4,56.....
Try it like this: np.savetxt("filename.txt", your_array, fmt="%d")

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.