user2095624 - 7 months ago 48

Python Question

Can someone please explain?

`import numpy`

a = ([1,2,3,45])

b = ([6,7,8,9,10])

numpy.savetxt('test.txt',(a,b))

This script can save well the data. But when I am running it through a loop it can print all but cannot not save all. why?

`import numpy`

a = ([1,2,3,4,5])

b = ([6,7,8,9,10])

for i,j in zip(a,b):

print i,j

numpy.savetxt('test.txt',(i,j))

Answer

You overwrite the previous data each time you call `numpy.savetext()`

.

A solution, using a temporary buffer array :

```
import numpy
a = ([1,2,3,4,5])
b = ([6,7,8,9,10])
out = []
for i,j in zip(a,b):
print i,j
out.append( (i,j) )
numpy.savetxt('test.txt',out)
```