Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python savetxt以int形式写入_Python_Arrays_File Io_Numpy - Fatal编程技术网

Python savetxt以int形式写入

Python savetxt以int形式写入,python,arrays,file-io,numpy,Python,Arrays,File Io,Numpy,我试图将一个数组写入一个文本文件,我希望每个元素都被写成int类型 我正在使用 np.savetxt(outfile_name, array, comments = '') 把文件写出来。我使用 array = array.astype(int) 当我在程序中打印array时,数组显示为int,但当我将其写入文本文件时,该文件如下所示: 0.000000000000000000e+00 1.000000000000000000e+00 1.000000000000000000e+00 1.0

我试图将一个数组写入一个文本文件,我希望每个元素都被写成
int
类型

我正在使用

np.savetxt(outfile_name, array, comments = '')
把文件写出来。我使用

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

您是否尝试过按指定格式


这使用的是标准。

您是否尝试过根据指定格式


这使用了标准。

我注意到,当数组包含“0.3”时,输出结果是“1”而不是“0”。您知道如何更改它,使结果向下舍入为0-0.4,向上舍入为0.5-0.9吗?可能最简单的方法是从数组中减去
0.5
。使用而不是
aType
我注意到,当数组包含“0.3”时,输出结果是“1”而不是“0”。您知道如何更改它,使结果向下舍入为0-0.4,向上舍入为0.5-0.9吗?可能最简单的方法是从数组中减去
0.5
。使用而不是
aType
np.savetxt(outfile_name, array, fmt="%d", comments='')
                              # ^ format as signed decimal integer