其他分享
首页 > 其他分享> > 如何在txt文件上只写一个整数numpy 2D数组

如何在txt文件上只写一个整数numpy 2D数组

作者:互联网

我知道如何使用numpy.savetxt()编写txt文件,但我无法使用整数编写文件.我有以下代码:

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

这就是我得到的矩阵的样子:

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

我正在寻找的是这样的:

29 329 1
43 108 1
43 195 1
56 151 1
56 97 1

解决方法:

你必须添加一个额外的参数

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

这只是实际数字的形成.

标签:python,numpy,writefile
来源: https://codeday.me/bug/20190516/1117018.html