whitespace within a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    whitespace within a string

    Is there a function/module that can be used to throw out extra whitespace
    that appears within a string? The problem that I have is this: Before any
    configuration is done to my files, they have lines with tabs in between the
    words like this:

    "disable = yes"

    After configuring the files using the operating system's administration
    tools, the OS rewrites the files to contain spaces instead of tabs. So now,
    the file looks like this:

    "disable = yes"

    I would like my script to work in either situation. Right now, it only works
    with spaces, not tabs. Below is the script:

    def enable_ssh():
    # works on 10.3 systems, not 10.2 systems
    import os
    os.chdir('/etc/xinetd.d')
    find = 'disable = yes'
    replace = 'disable = no'
    x = file('ssh')
    data = x.read()
    x.close()
    search = str.find(data, find)
    if search >= 0:
    data = data.replace(fi nd, replace)
    outputFile = file('ssh', 'w')
    outputFile.writ e(data)
    outputFile.clos e()
    print
    print "SSH was disabled... service restarted!!!"
    print
    else:
    print
    print "SSH was enabled... no action taken."
    print
    enable_ssh()
  • Jeff Epler

    #2
    Re: whitespace within a string

    You can use the magic of no-arg split() to do this:
    def canonize_whites pace(s):
    return " ".join(s.split( ))
    [color=blue][color=green][color=darkred]
    >>> canonize_whites pace("a b\t\tc\td\t e")[/color][/color][/color]
    'a b c d e'

    A regular expression substituion can do the job too
    def canonize_whites pace(s):
    return re.sub('\s+', ' ', s)
    [color=blue][color=green][color=darkred]
    >>> canonize_whites pace("a b\t\tc\td\t e")[/color][/color][/color]
    'a b c d e'

    Of course, if 'x=y' is accepted just like 'x = y' and 'x = y', then
    neither of these approaches is good enough.

    def canonize_config _line(s):
    if not '=' in s: return s
    a, b = s.split("=", 1)
    return "%s = %s" % (a.strip(), b.strip())[color=blue][color=green][color=darkred]
    >>> [canonize_config _line(s) for s in[/color][/color][/color]
    ... ['x=y', 'x\t= y', ' x = y ', "#z"]]
    ['x = y', 'x = y', 'x = y', '#z']

    Jeff

    Comment

    • Erik Max Francis

      #3
      Re: whitespace within a string

      Bart Nessux wrote:
      [color=blue]
      > Is there a function/module that can be used to throw out extra
      > whitespace
      > that appears within a string? The problem that I have is this: Before
      > any
      > configuration is done to my files, they have lines with tabs in
      > between the
      > words like this:
      >
      > "disable = yes"[/color]

      If you realy want to collapse any whitespace at all into a single space
      character, how about:

      ' '.join(S.split( ))

      --
      __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      \__/ One sword keeps the other in the sheath.
      -- George Herbert

      Comment

      • Bart Nessux

        #4
        Re: whitespace within a string

        Jeff Epler wrote:[color=blue]
        > You can use the magic of no-arg split() to do this:
        > def canonize_whites pace(s):
        > return " ".join(s.split( ))
        >[color=green][color=darkred]
        > >>> canonize_whites pace("a b\t\tc\td\t e")[/color][/color]
        > 'a b c d e'
        >
        > A regular expression substituion can do the job too
        > def canonize_whites pace(s):
        > return re.sub('\s+', ' ', s)
        >[color=green][color=darkred]
        > >>> canonize_whites pace("a b\t\tc\td\t e")[/color][/color]
        > 'a b c d e'
        >
        > Of course, if 'x=y' is accepted just like 'x = y' and 'x = y', then
        > neither of these approaches is good enough.
        >
        > def canonize_config _line(s):
        > if not '=' in s: return s
        > a, b = s.split("=", 1)
        > return "%s = %s" % (a.strip(), b.strip())[color=green][color=darkred]
        > >>> [canonize_config _line(s) for s in[/color][/color]
        > ... ['x=y', 'x\t= y', ' x = y ', "#z"]]
        > ['x = y', 'x = y', 'x = y', '#z']
        >
        > Jeff
        >[/color]

        Thanks for the tip. I'll give it a try.

        Comment

        Working...