Python 3 Script to Replace Spaces with New Lines in Text File Full Project For Beginners

Python 3 Script to Replace Spaces with New Lines in Text File Full Project For Beginners

 

Welcome folks today in this blog post we will be replacing spaces with new lines in text file.All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

app.py

 

f = open("test.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()

 

 

Now if you execute the python script by typing the below command as shown below

 

See also  Python 3 Script to Remove Blank or Empty Lines From Text File Full Project For Beginners

python app.py

 

 

So here as you can see inside the text file we have some text written and we have some spaces and we will be replacing these spaces with new line character as shown below

 

So just create a test.txt file and write some text into it

 

 

And after execution of the script it will create the output file and then it will contain the new line characters

 

 

Leave a Reply