How to format a text file(remove unwanted line spacing and paragraph spacing) in python?

  file-handling, python, python-3.x, split, string

I have extracted an email and save it to a text file that is not properly formatted.

The file looks like this:

                Hi Kim,
               



                     Hope you are fine.
                



                  Your Code is:
                 

                    42483423



                 Thanks and Regards,
                        

                    Bolt

I want to open and edit this file and arrange it in a proper format removing all the spaces before the text and below the text in the proper format like:

Hi Kim,
Hope you are fine.
Your Code is:
42483423
Thanks and Regards,
Bolt

My start procedure,

file = open('email.txt','rw')

Source: Python Questions

LEAVE A COMMENT