0

波纹管是从两个 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

4

1 回答 1

1

可能有比 for 循环更优雅的方式来组合你的数组,以及更优雅的方式来写入文件。但是使用您的代码,您应该能够替换最后一行以获得所需的结果:

将其替换为outfile.write('%d %d %d\n' % (int(plate2[i]), int(mjd2[i]), int(fiber2[i])))

于 2014-04-12T17:44:29.387 回答