Is there a way to unindent all indented lines in a text file? The contents of the text file will change so I don’t want to specify exact lines as it will be different each month.
Here is an example of my text file:
(text.txt)
Adam
Mary
Toni
Ryan
Peter
Ashley
Cam
Alex
Wanted output:
Adam
Mary
Toni
Ryan
Peter
Ashley
Cam
Alex
I am currently using the following code to open/rw (modify the file). I have it removing empty lines.
with open(‘text.txt’, ‘r+’) as f:
lines = f.readlines()
f.seek(0)
f.writelines(line for line in lines if line.strip())
f.truncate()
Output:
Adam
Mary
Toni
Ryan
Peter
Ashley
Cam
Alex
Researching a bit before asking, I found some stuff on textwrap.dedent. I tried to work it out and add it to my code but was unsuccessful.
Does anyone know how to properly add this or of another way?
Thank you! :)