user2590144
user2590144

Reputation: 281

Python savetxt write as int

I am trying to write an array out to a text file and I want each element to be written as an int type.

I am using

np.savetxt(outfile_name, array, comments = '')

to write out the file. I converted array from float to int using

array = array.astype(int)

When I printed array in the program, the array came out as int, but when I wrote it to a text file, the file looks like:

0.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00

Upvotes: 1

Views: 4251

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 121987

Have you tried specifying a format, per the documentation?

np.savetxt(outfile_name, array, fmt="%d", comments='')
                              # ^ format as signed decimal integer

This uses the standard format specification mini-language.

Upvotes: 9

Related Questions