I'm very new to python and having trouble with this bit of code that I wrote:
#! /usr/bin/env python
OutFileName = "k_values.txt"
OutFile = open(OutFileName, 'w')
with open("structure_working.txt") as infile:
for Line in open("structure_working.txt"):
Line = Line.strip('\n')
ElementList = Line.split('\t')
k1 = ElementList[2]
k2 = ElementList[3]
k3 = ElementList[4]
k4 = ElementList[5]
k = 1
if k2 > k:
k = 2
if k3 > k2:
k = 3
if k4 > k3:
k = 4
name = ElementList[1]
OutputString = "%s\t%s" % (name, k)
print OutputString
OutFile.write(OutputString + "\n")
OutFile.close()
My input file is a tab delimited file. The problem is that my for loop only runs once over the header line and doesn't continue into the body of the file. Here is an example of my text file:
num indiv 1 2 3 4 k
1 JB1972 0.642 0.141 0.091 0.127
2 JB1973 0.754 0.113 0.079 0.055
3 JB1974 0.069 0.422 0.418 0.091
4 JB1976 0.175 0.339 0.249 0.237
5 JB1977 0.149 0.365 0.383 0.103
6 JB1978 0.421 0.184 0.146 0.249
7 JB1979 0.264 0.246 0.395 0.095
8 JB1980 0.074 0.511 0.287 0.128
9 JB1981 0.083 0.162 0.275 0.48
10 JB1982 0.059 0.145 0.73 0.067
None of the answers I've found to the problem "for loop only runs once" were helpful to my specific problem. The fact that the code works on the header line makes me think that the problem is with the for loop. Any ideas?