I have searched this on Stackoverflow and all of the "duplicates" for this topic but it seems remained unanswered. I have tried all these:
Attempt#1:
for word in header:
writer.writerow([word]
Pasted from writing data from a python list to csv row-wise
Attempt#2:
And this one, should have been close but it has a bug:
# Open a file in witre mode
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
Pasted from <http://www.tutorialspoint.com/python/file_writelines.htm>
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
fo.seek(0, 2)
line = fo.writelines( seq )
# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
line = fo.next()
print "Line No %d - %s" % (index, line)
# Close opend file
fo.close()
Pasted from http://www.tutorialspoint.com/python/file_writelines.htm
Attempt#3:
>>>outF = open("myOutFile.txt", "w")
>>>for line in textList:
... outF.write(line)
... outF.write("\n")
>>>outF.close()
Pasted from http://cmdlinetips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/
Attempt#4:
with open('file_to_write', 'w') as f:
f.write('file contents')
Pasted from Correct way to write line to file in Python
Attempt#5:
This one which uses the append when writing to file.. but it appends each line at the end of each row. So it would be hard for me to separate all the rows.
append_text = str(alldates)
with open('my_file.txt', 'a') as lead:
lead.write(append_text)
Pasted from Python: Saving a string to file without overwriting file's contents
Can someone help me how to write a newline of row per iteration in a loop to a file without overwriting the file?