I am trying to append new information in a for loop to a new line and then write it to a text file. A simple sample code is giving me issues:
ID = "A1"
val = 0.4
tmp = []
for i in range(0,10):
aa = (ID + ',' + repr(val))
tmp.append(aa)
text_file = open("output.txt", "w")
text_file.write("%s\n" % tmp)
However, the contents of 'output.txt' is:
['A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4', 'A1,0.4']
Whereas I would want each iteration within the loop on a newline:
['A1,0.4'
'A1,0.4'
...
...
'A1,0.4']
I know adding a newline '\n' should work, but it is simply adding this as text and not working as a newline. Some of these files are 1000's of lines long, so would opening the text file and writing the output each time be very time consuming as opposed to creating one, large array and output that to the text file in the end?