Sunday, December 2, 2018

Python write line by line to a text file





I am trying to output the result from a Python script to a text file where each output should be saved to a line.



f1=open('./output.txt', 'a')
f1.write(content + "\n")



When I open output.txt with the regular notepad the results look like this:



color amber color aqua color analysis color app color adobe color alive app


However, when I open the file with notepad++ it looks fine and each word is saved on a line.



How can make the script save the result line-by-line so it would show the same on the regular notepad?


Answer




Well, the problem you have is wrong line ending/encoding for notepad. Notepad uses Windows' line endings - \r\n and you use \n.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...