1

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
pyboy1995
  • 25
  • 1
  • 9
  • firstly, you didn't `f.close()` your file. secondly, I believe you can directly write the line `f.write(line2)`, you don't need to iterate it. – WiseDev Aug 07 '19 at 06:01
  • also, you have two seperate for loops where in the first for loop, you keep overwriting `line2`. So the only line2 that gets written is the last one in the loop.. – WiseDev Aug 07 '19 at 06:03
  • Possible duplicate of [Read and overwrite a file in Python](https://stackoverflow.com/questions/2424000/read-and-overwrite-a-file-in-python) – ComplicatedPhenomenon Aug 07 '19 at 06:03

5 Answers5

3
with open("testfile.txt", "r") as r:
    with open("testfile_new.txt", "w") as w:
        w.write(r.read(.replace(' ' , '\n'))
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
Alex Z
  • 76
  • 3
  • 1
    Note that this slurps the entire file into memory. Also, you can use `with open('testfile.txt', 'r') as r, open('testfile_new.txt', 'w') as w:` and avoid a block. There's a syntax error as well: `r.read(.replace(' ' , '\n')` should be `r.read().replace(' ', '\n')`. Consider explaining why `with`... is advantageous over `open()` and/or offering a few words about what OP did wrong in their code attempt. – ggorlen Aug 07 '19 at 06:08
  • that will not work!!! it creates a new file and that not what the OP wants – LinPy Aug 07 '19 at 06:24
1

Example:

with open("file1.txt", "r") as read_file:
    with open("file2.txt", "w") as write_file:
        write_file.write(read_file.read().replace(" ", '\n'))

Content of file1.txt:

15.9 17.2 18.6 10.5

Content of file2.txt:

15.9
17.2
18.6
10.5

NOTE:

Or you can use the split and join method instead of replace.

write_file.write("\n".join(read_file.read().split()))
milanbalazs
  • 4,811
  • 4
  • 23
  • 45
0

try like this instead

f = open("testfile.txt", "r")
text=f.read()
f.close()
f=open("testfile.txt", "w+")
text2=''
if ' ' in text:
    text2 = text.replace(' ' , '\n')
    print(text2)
    f.write(text2)
f.close()
3NiGMa
  • 545
  • 1
  • 9
  • 24
0

You can try using string replace:

string = string.replace('\n', '').replace('\r', '')

Firstly: f.close() is not there.

Secondly: try above code. It will replace spaces to new lines.

Community
  • 1
  • 1
Abdullah Akhtar
  • 529
  • 5
  • 14
0

Use str.split with str.join

Ex:

with open("testfile.txt", "r") as infile:
    data = infile.read()

with open("testfile.txt", "w") as outfile:
    outfile.write("\n".join(data.split()))
Rakesh
  • 81,458
  • 17
  • 76
  • 113