5

I know how to write the txt file using numpy.savetxt() but I can't get it to write the file using just integers. I have the following code:

new_picks = new_picks.astype(int)
np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int))

This is how the matrix that I'm getting looks like:

2.900000000000000000e+01 3.290000000000000000e+02 1.000000000000000000e+00
4.300000000000000000e+01 1.080000000000000000e+02 1.000000000000000000e+00
4.300000000000000000e+01 1.950000000000000000e+02 1.000000000000000000e+00
5.600000000000000000e+01 1.510000000000000000e+02 1.000000000000000000e+00
5.600000000000000000e+01 9.700000000000000000e+01 1.000000000000000000e+00
7.000000000000000000e+01 2.840000000000000000e+02 1.000000000000000000e+00
3.500000000000000000e+01 3.170000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 2.110000000000000000e+02 1.000000000000000000e+00
6.400000000000000000e+01 1.180000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 3.700000000000000000e+01 1.000000000000000000e+00
1.300000000000000000e+01 1.950000000000000000e+02 1.000000000000000000e+00
1.300000000000000000e+01 1.680000000000000000e+02 1.000000000000000000e+00
1.300000000000000000e+01 2.780000000000000000e+02 1.000000000000000000e+00
4.900000000000000000e+01 2.200000000000000000e+01 1.000000000000000000e+00
4.900000000000000000e+01 1.040000000000000000e+02 1.000000000000000000e+00
4.900000000000000000e+01 7.500000000000000000e+01 1.000000000000000000e+00
5.400000000000000000e+01 2.610000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 2.600000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 1.150000000000000000e+02 1.000000000000000000e+00
5.400000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00
1.300000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00
4.900000000000000000e+01 5.400000000000000000e+01 1.000000000000000000e+00

What I'm looking for is something like this:

29 329 1
43 108 1
43 195 1
56 151 1
56 97 1
Diego Aguado
  • 1,557
  • 15
  • 32

2 Answers2

6

You have to add an extra parameter in

savetxt(fname='newPicksData.txt', X=new_picks.astype(int), fmt ='%.0f\n')

that is just the formating of the actual number.

Anjum....
  • 3,545
  • 1
  • 30
  • 41
2

You have to specify the format separate using the fmt argument, see the documentation here http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html

You have to use the following syntax to get what you want:

np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int),fmt="%i")

Omitting that argument, defaults it to fmt='%.18e', which is exactly what you see in the output that you posted in the question.


Furthermore, do you really need the astype(int) part in your code? The np.savetext command can format strings/doubles perfectly fine into int without changing the data yourself. This can be demonstrated with the below code chunk:

import numpy
x = numpy.array([1.3,2.1,3.9,4.2,5.5,6.1])
numpy.savetxt(fname='newPicksData.txt', X=x, fmt="%i")

The listed code chunk then yields the following text file:

1
2
3
4
5
6
Bas Jansen
  • 3,010
  • 4
  • 26
  • 60