I have a text file which looks like this:
15.9 17.2 18.6 10.5
I want to edit this file in Python so that it looks like this:
15.9
17.2
18.6
10.5
This means that I need to replace the space strings by newline strings and save the text.
I tried this but it doesn't work:
f = open("testfile.txt", "w")
for line in f:
if ' ' in line:
line2 = line.replace(' ' , '\n')
print(line2)
for i in line2:
f.write(line2(i))
f.close
The print for line2
is already working, but I don't get a new text file with spaces replaced by newlines.
How can I fix the problem and produce the desired output?