使用outfile编写一个没有小数点到另一个文件的值
-
21-12-2019 - |
题
波纹管是一个代码,从两个txt文件中取值,以使另一个txt文件
plate1, mjd1, fiber1, d1, pval1 = np.loadtxt('combine3ga.txt', unpack='True')
plate2, mjd2, fiber2, d2, pval2 = np.loadtxt('combine4ga.txt', unpack='True')
with open('ekek2.txt', 'w') as outfile:
for i in range(len(plate1)):
if d2[i] < d1[i] and pval2[i] > 0.8:
print plate2[i], mjd2[i], fiber2[i], d2[i], pval2[i]
with open('ekek2.txt', 'a') as outfile:
outfile.write('{0} {1} {2}\n'.format(plate2[i], mjd2[i], fiber2[i]))
.
输出文件的前几行看起来像
1958.0 53385.0 614.0
2214.0 53794.0 308.0
436.0 51883.0 634.0
我正在尝试制作它,以便这些值是整个数字,而不是像
一样的小数1958 53385 614
2214 53794 308
436 51883 634
解决方案
将阵列与循环组合的阵列可能更优雅,更优雅的方式写入文件。但是使用您的代码,您应该能够更换最后一行以获取所需的结果:
用outfile.write('%d %d %d\n' % (int(plate2[i]), int(mjd2[i]), int(fiber2[i])))
替换它
不隶属于 StackOverflow