#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12

    snow-white spaces!


    Hi,
    Suppose I've a large text file and I want to wipe out all the whitespaces present in it....this cld be done using replace but then it wld be a daunting task to remove all the whitespaces if I sit to write separate replaces for \t,\n,spaces,etc.....

    Is there any way by which I can achieve it at one go???

    Thanks & Rgds,
    Subha
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Use a regular expression perhaps?
    Code:
    #!/usr/bin/env python 
    import re
    
    reg = re.compile('\s+')
    line = """   This is a    line with                                             
                lots of spaces   and tabs and                                       
                new lines """
    
    print "BEFORE REPLACEMENT"
    print line
    
    print "\nAFTER REPLACEMENT"
    line2 = reg.sub("", line)
    print line2
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just to give you the option, here are a few other ways. It's not as bad as you might have thought .

    Code:
    >>> from string import whitespace as whitespaces
    >>> 
    >>> sample = 'Hello World!'
    >>> 
    >>> for character in whitespaces:
    ...     sample = sample.replace(character, '')
    ... 
    >>> sample
    'HelloWorld!'
    >>> 
    >>> sample = 'Hello World!'
    >>>
    >>> results = []
    >>>    
    >>> for letter in sample:   
    ...     if letter not in whitespaces: results.append(letter)
    ... 
    >>> results
    ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!']
    >>>
    >>> ''.join(results)
    'HelloWorld!'
    >>>
    >>> [letter for letter in sample if letter not in whitespaces]
    ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!']
    >>>
    Particularly fond of the last one, it just reads so nicely . Enjoy!

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo