-1

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?

Community
  • 1
  • 1
athrun
  • 29
  • 9

2 Answers2

1
data = [1,2,3,4,5]
with open('asd.txt', 'w') as fn:
    for i in data:
        fn.write(str(i) + '\n') # Add a \n (newline) so the next write will occure in the next line

Content of asd.txt:

1
2
3
4
5

If you want to append to a file use with open('asd.txt', 'a') as fn:

Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56
0

There is 2 ways to do that:

First by adding '\n' character at the end of your output line :

for x in row:
   writer.write(x + "\n")

Second by opening file in Append mode, it is going to add lines existing text file but be careful.it's not overwrite the file :

fw = open(myfile.txt,'a')
Reza Tanzifi
  • 572
  • 4
  • 13
  • yes i have tried those but appending only adds a row at the end of another row.. thus its difficult to segregate the data if i have thousands of them – athrun Aug 26 '16 at 09:34
  • What do you mean about can not separating rows?? – Reza Tanzifi Aug 26 '16 at 14:30
  • your code actually worked with the 'a' and '\n' for lines of rows to be written per iteration in a loop. thanks! ...but i did not use the for-loop 'for x in row:' – athrun Aug 26 '16 at 15:20