Python is written 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 stored in a string.

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

When I open output.txt with the usual notepad , the results are as follows:

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

However, when I open the file with notepad++ , it looks great and every word is saved in a line.

How to make a script save the result in turn so that it shows the same thing in a regular notepad?

+5
source share
2 answers

Well, the problem you have is the wrong string / encoding for notepad. Notepad uses the final lines of Windows - \r\n , and you use \n .

+6
source

You might want to look at line-dependent line separators, for example:

 import os with open('./output.txt', 'a') as f1: f1.write(content + os.linesep) 
+5
source

Source: https://habr.com/ru/post/1244056/


All Articles