- Code: Select all
#list required arguments and open files
print "Enter program_name, infile, outfile, reagent"
infile = open(sys.argv).readlines()
outfile = open(sys.argv, 'w')
#save infile header line to hline1 and split into parts
hline1 = infile.pop(0)
hparts = hline1.split('\t')
reagent = sys.argv
#write outfile header line
outfile.write('value' + '\t' + 'reagent' + '\t' + 'patient' + '\t' + 'antigen' + '\n')
# read matrix rows and split into parts
for line in infile:
line = line.rstrip()
parts = line.split('\t')
patient = parts
#Use loop to print parts to outfile
for i in range(1,len(parts)):
outfile.write(parts[i] + '\t' + reagent + '\t' + patient + '\t' + hparts[i] + '\n')
#print reference line
print "The file been written to", outfile
I've attached a sample test1.txt file and a testout.txt file (without the extra newlines to show what I want).
Thanks in advance!