简体   繁体   中英

how can I save more than one data in a single out put file separately using numpy

How can I save more than one data consecutively in a single output file using numpy ? For example, the out put file should contain

Matrix A 
"and below that some text and the another"
matrix B  

You simply pass an open file handle to numpy.savetxt() :

In [1]: import numpy as np

In [2]: a = np.zeros((3,3))

In [3]: b = np.ones((5,6))

In [4]: with open('mix.txt', 'w') as f:
   ...:     np.savetxt(f, a, fmt='%g')
   ...:     f.write('my comment followed by a new array:\n')
   ...:     np.savetxt(f, b, fmt='%g')
   ...:     

In [5]: !more mix.txt
0 0 0
0 0 0
0 0 0
my comment followed by a new array:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM