Python savetxt写为int

时间:2014-04-20 22:34:04

标签: python arrays file-io numpy

我正在尝试将数组写入文本文件,我希望每个元素都被写为int类型。

我正在使用

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

写出文件。我使用

arrayfloat转换为int
array = array.astype(int)

当我在程序中打印array时,数组显示为int,但是当我将其写入文本文件时,文件看起来像:

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

1 个答案:

答案 0 :(得分:7)

您是否尝试过按the documentation指定格式?

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

这使用标准format specification mini-language