Python Forum
does anyon want to write an untabify command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
does anyon want to write an untabify command?
#10
(Sep-08-2019, 06:17 PM)Skaperen Wrote: i was expecting some existing function that just does it.
Yes with your none exciting effort,you should except that Dodgy

Skaperen Wrote:i like that in_place module. too bad i can't assume every python installation has it. and i wish they would quit naming things with CamelCase.
There is a option in standard library fileinput with inplace=True,but it not so nice to use,in-place module dos it better and are simpler to use.

As mention all code is there,here taken out.
import re
import in_place

def tab_space(text, tabs=8):
    result = ''
    for c in text:
        if c == '\t':
            result += ' '
            while len(result) % tabs != 0:
                result += ' '
        else:
            result += c
    return result

def tab_even(file, space=4):
    with in_place.InPlace(file) as f:
        text = f.read()
        result = re.sub(r'\s\s+|\t', ' '*space, text)
        f.write(result)

if __name__ == '__main__':
    #--- Tab space ---#
    file = 'file_1.txt'
    tabs = 4
    with in_place.InPlace(file) as f_out:
        for line in f_out:
            line = tab_space(line, tabs)
            f_out.write(line)

    #--- Even Space ---#
    #file = 'file_1.txt'
    #tab_even(file, space=8)
Test file file_1.txt.
Output:
One Two Three Four Five six seven One Two Three Four Five six seven One Two Three Four Five six seven
Reply


Messages In This Thread
RE: does anyon want to write an untabify command? - by snippsat - Sep-09-2019, 05:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  function to untabify Skaperen 3 1,406 Aug-14-2022, 12:49 AM
Last Post: Skaperen
  a command to write i just thought of Skaperen 0 1,366 Apr-12-2020, 03:18 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020