I wrote a script to reformat a tab-delimited matrix (with header) into a “long format”. See example below. It performs the task correctly but it seems to get stuck in an endless loop…
Example of input:
WHO THING1 THING2
me me1 me2
you you1 you2
Desired output:
me THING1 me1
me THING2 me2
you THING1 you1
you THING2 you2
Here is the code:
import csv
matrix_file = open('path')
matrix_reader = csv.reader(matrix_file, delimiter="\t")
j = 1
while j:
matrix_file.seek(0)
rownum = 0
for i in matrix_reader:
rownum+=1
if j == int(len(i)):
j = False
elif rownum ==1:
header = i[j]
else:
print i[0], "\t",header, "\t",i[j]
j +=1
I think it has to do with my exit command (j = False). Any ideas?
edit: Thanks for suggestions. I think a typo in my initial posting led to some confusion, sorry about that For now I have employed a simple solution:
valid = True
while valid:
matrix_file.seek(0)
rownum = 0
for i in matrix_reader:
rownum+=1
if j == int(len(i)):
valid = False
etc, etc, etc...
j+=1is outside the while loop as senderle’s answer says.other improvements can be:
int(len(i)),just uselen(i),aslen()always returns a int so no need ofint()aroundit
for rownum,i in enumerate(matrix_reader):so now there’s noneed of handling an extra variable
rownum, it’ll be incremented byitself.
EDIT: A working version of your code, I don’t think there’s a need of
whilehere, theforloop is sufficient.