Tuesday, 27 August 2013

Need to write the contents on next column of file for elif condition result?

Need to write the contents on next column of file for elif condition result?

I am reading from a file and checking the numbers in the second column. I
have perform the following checks:
Number is less than 0.20
Number is less than 0.30
Number is less than 0.40
Number is less than 0.50
If condition one is true write the value which satisfies the condition as
the first column in an output file.
If condition two is true write the which satisfies the condition as the
second column in the same output file.
If condition three is true write the value which satisfies the condition
as colunm3 in the the same output file.
If condition four is true write the value which satisfies the condition as
colunm4 in the the same output file.
This is what I have so far:
f = open('outfilename','r')
d = open('newfile','w')
lines = f.readlines()
for line in lines:
job = line.split()
if(float(job[2]) < 0.20):
d.write(str(job[2]))
d.write('\n')
elif(float(job[2]) < 0.30):
d.write(str(job[2]))
d.write('\n')
elif(float(job[2]) < 0.40):
d.write(str(job[2]))
d.write('\n')
elif(float(job[2]) < 0.50):
d.write(str(job[2]))
d.write('\n')
d.close()
f.close()
But I am getting this output:
0.061
0.0
0.012
0.0
0.079
0.03
0.109
0.044
0.019
0.035
0.018
0.019
0.004
0.147
0.111
0.184
0.121
0.005
0.299
0.091
0.077
0.245
0.345
0.323
0.456
0.399
0.499
Can someone help me figure out what is wrong with my code?

No comments:

Post a Comment