[Python] Text-formatting: Replace tabs with spaces in text/config file and keep the pretty...

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    not sure how to categorize the following issue, I thought about relating it to BASH or Python first, but actually it's rather language-independent imo...

    Anyway, I was searching but couldn't find any really useful solution for the following problem: I have a lot of files which contain usually tabs and were formatted according to those by the original authors. I want to replace them with spaces, but without loosing the pretty formatting (respectively I want re-formatting them to fit with spaces) the usual method by Ctrl+R in most editors leads to an ugly formatting as you can see on the pictures below (ignore the variable names and numbers).

    Before I was starting to write some script to format them the way I want to I thought that this problem must be annoyed many other people as well, so I wouldn't be surprised if this could be achieved via some combination of Linux Shell/BASH commands? I've tried column and other commands, but without luck...

    To be clear: I need a solution which can be "automatized", so importing such file into a LibreOffice table manually and exporting it from there (manually as well) isn't a satisfying solution because the amount of files demands to be batch-edited with a script (I'd prefer this) or with some program.

    Before editing/replacing tabs with spaces:

    [​IMG]

    After simple replacing all tabs with spaces via Ctrl+R:

    (the red square shows the original look / lost formatting after replacing, the green one shows how it should ideally look - I've edited those lines by hand)

    [​IMG]

    Just in case...ignore the names and assigned values/comments, there are other files full of tabs looking very different, so the shown code is just an example to illustrate what I mean.

    Any help, approaches and ideas are highly appreciated; thank you in advance! :)

    Continue reading...

Compartilhe esta Página