How to write a single integer 2D array in a txt file

I know how to write a txt file using numpy.savetxt() , but I cannot get it to write a file using only integers. I have the following code:

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

This is what the matrix I get 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 

I am looking for something like this:

 29 329 1 43 108 1 43 195 1 56 151 1 56 97 1 
+5
source share
2 answers

You need to add an extra parameter to

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

it is simply the formation of the actual number.

+4
source

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

To obtain the desired result, you must use the following syntax:

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

Omitting this argument, the default value is fmt='%.18e' , which is what you see in the output published in the question.


Also, do you really need the astype(int) part in your code? The np.savetext command can format strings / doubles perfectly in int without changing the data itself. This can be demonstrated using the code snippet below:

 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") 

Then the specified code fragment displays the following text file:

 1 2 3 4 5 6 
+2
source

All Articles