I’m writing a solution in MATLAB but am using Python to parse a text file – Python is definitely not my strong point. Basically, I’ve managed to parse my text file for relevant lines and would like these exported as a tab delimited text file in the following format:
num1 num2 num3 num4 num5
num1 num2 num3 num4 num5
num1 num2 num3 num4 num5
num1 num2 num3 num4 num5
However, at present, my output file looks like this:
[num3, num1, num2, num4, num5], [num3, num1, num2, num4, num5], [num3, num1, num2, num4, num5], [num3, num1, num2, num4, num5]
My code looks like this:
for <blah blah>
num3,num1,num2,num4,num5 = data
outputData.append(data)
outfile.write("%s"%(outputData))
How do I rearrange the variables and have them output into a nice structured array?
Try the following:
The code
'\t'.join(map(str, [num1, num2, num3, num4, num5]))will result in a tab-delimited string with the data in the correct order, sooutputDatawill be a list of those strings. Then'\n'.join(outputData)will combine all of the lines fromoutputDatawith a newline separating each line, which is what you want to write to the file.Alternatively you could get rid of
outputDataentirely and do something like this: