我正在尝试将数组写入文本文件,我希望每个元素都被写为int
类型。
我正在使用
np.savetxt(outfile_name, array, comments = '')
写出文件。我使用
将array
从float
转换为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
答案 0 :(得分:7)
您是否尝试过按the documentation指定格式?
np.savetxt(outfile_name, array, fmt="%d", comments='')
# ^ format as signed decimal integer