whitespace within a string

Bart Nessux bart_nessux at hotmail.com
Tue Feb 24 08:28:12 EST 2004


Jeff Epler wrote:
> You can use the magic of no-arg split() to do this:
>     def canonize_whitespace(s):
>         return " ".join(s.split())
> 
>     >>> canonize_whitespace("a  b\t\tc\td\t e")
>     'a b c d e'
> 
> A regular expression substituion can do the job too
>     def canonize_whitespace(s):
>         return re.sub('\s+', ' ', s)
> 
>     >>> canonize_whitespace("a  b\t\tc\td\t e")
>     '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())
>     >>> [canonize_config_line(s) for s in
>     ...        ['x=y', 'x\t=  y', '  x =    y ', "#z"]] 
>     ['x = y', 'x = y', 'x = y', '#z']
> 
> Jeff
> 

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




More information about the Python-list mailing list