Search code examples
python-2.7numpyinto-outfile

Writing values with no decimal points to another file using outfile


bellow is a code that takes values from two txt files to make another txt file

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]))

The first few lines of the output file look like

1958.0 53385.0 614.0

2214.0 53794.0 308.0

436.0 51883.0 634.0

I am trying to make it so that the values are whole numbers not including decimals just like

1958 53385 614

2214 53794 308

436 51883 634


Solution

  • There may be more elegant ways to combine your arrays than a for loop and more elegant ways to write to a file. But using your code, you should be able to replace the last line to obtain the desired result:

    Replace it with outfile.write('%d %d %d\n' % (int(plate2[i]), int(mjd2[i]), int(fiber2[i])))