0

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! :)

hackerboi
  • 31
  • 5
  • 2
    "I tried to work it out and add it to my code but was unsuccessful." We can only possibly tell you what is wrong with code that you actually show to us. That said, the problem becomes easier to [research](https://meta.stackoverflow.com/questions/261592) if you think about it differently: what you're removing isn't "indentation" - because it could be *any* amount, not necessarily aligning to any standard, and it doesn't have *meaning*. It's just *leading whitespace*. That let me find the linked duplicate almost immediately with the site search. – Karl Knechtel May 26 '22 at 20:45
  • 1
    In fact, you already knew what to do in the original code. Think carefully about the line: `f.writelines(line for line in lines if line.strip())`. How does the `if line.strip()` test work? Do you see how the *actual result* from `line.strip()` (not just the boolean evaluation inside an `if` condition) lets you solve the problem? – Karl Knechtel May 26 '22 at 20:46
  • 1
    @hackerboi it's not a bad thing that your question was closed – it's a common occurrence because so many questions have been asked before, and it's best for stackoverflow to avoid having duplicate questions with duplicate answers when possible. for the record, it's appreciated that you did some research before asking – Derek O May 26 '22 at 20:47
  • I see it is linked too another question. I just read through it and saw a link to the docs. But doesn’t .strip remove all the leading and trailing whitespace characters (including tabs and spaces) to begin with? I assumed my line f.writelines(line for line in lines if line.strip()) it would remove the indention or spaces as well. @KarlKnechtel – hackerboi May 26 '22 at 21:16
  • I’m sorry, I am still learning so I don’t fully understand. – hackerboi May 26 '22 at 21:17
  • 1
    Nothing you do in Python can modify a `str` object in place - it only creates a *new* string. So the `if` test makes that new string, uses it to decide whether the line was blank, and then throws it away. If you want to write the `line.strip()` result to the file, then it also needs to say `line.strip()` on the left-hand side of the `for`. Or you can do two separate passes over the data, or make clever use of the `:=` operator in 3.8 and up. – Karl Knechtel May 26 '22 at 22:26
  • @KarlKnechtel ahhh yes and then I would just need to clarify within the strip which elements. So for it to strip tabs and spaces from the file. Didn’t think about that. Thank you so much! – hackerboi May 26 '22 at 23:27

0 Answers0