如何在 txt 文件上写一个只有整数的 numpy 二维数组
How to write a only integers numpy 2D array on a txt file
我知道如何使用 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
您必须使用 fmt
参数单独指定格式,请参阅此处的文档 http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
您必须使用以下语法来获得您想要的内容:
np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int),fmt="%i")
省略该参数,默认为 fmt='%.18e'
,这正是您在问题中发布的输出中看到的内容。
此外,您的代码中真的需要 astype(int)
部分吗? np.savetext
命令可以将 strings/doubles 完美地格式化为 int
而无需您自己更改数据。这可以用下面的代码块来演示:
import numpy
x = numpy.array([1.3,2.1,3.9,4.2,5.5,6.1])
numpy.savetxt(fname='newPicksData.txt', X=x, fmt="%i")
列出的代码块然后生成以下文本文件:
1
2
3
4
5
6
您必须在
中添加一个额外的参数
savetxt(fname='newPicksData.txt', X=new_picks.astype(int), fmt ='%.0f\n')
这只是实际数字的格式。
我知道如何使用 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
您必须使用 fmt
参数单独指定格式,请参阅此处的文档 http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
您必须使用以下语法来获得您想要的内容:
np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int),fmt="%i")
省略该参数,默认为 fmt='%.18e'
,这正是您在问题中发布的输出中看到的内容。
此外,您的代码中真的需要 astype(int)
部分吗? np.savetext
命令可以将 strings/doubles 完美地格式化为 int
而无需您自己更改数据。这可以用下面的代码块来演示:
import numpy
x = numpy.array([1.3,2.1,3.9,4.2,5.5,6.1])
numpy.savetxt(fname='newPicksData.txt', X=x, fmt="%i")
列出的代码块然后生成以下文本文件:
1
2
3
4
5
6
您必须在
中添加一个额外的参数savetxt(fname='newPicksData.txt', X=new_picks.astype(int), fmt ='%.0f\n')
这只是实际数字的格式。