[英]Writing numpy arrays to files
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: 我想知道
numpy
是否具有用于写入文件的任何内置函数,或者是否存在方法,该方法应用于编写像这样构造的数组:
[[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.
我确实知道如何使用for循环使用常规python列表来做到这一点,但我想知道应该采用哪种方式。
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 有关详细信息,请参见: http : //docs.scipy.org/doc/numpy-1.10.0/reference/genic/numpy.savetxt.html
[Update] You can use the formatting parameter eg like this: [更新]您可以使用格式参数,例如:
your_array = [[2, 3, 4], [3, 5, 6], [8, 7, 9]]
np.savetxt("filename.txt", your_array, fmt="%d")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.