Stripping whitespace
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Wed Jan 23 17:37:32 EST 2008
On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
> ryan k <ryan at ryankaskel.com> writes:
>> Hello. I have a string like 'LNAME
>> PASTA ZONE'. I want to create a list of those words and
>> basically replace all the whitespace between them with one space so i
>> could just do lala.split(). Thank you!
>
> import re
> s = 'LNAME PASTA ZONE'
> re.split('\s+', s)
Please tell me you're making fun of the poor newbie and didn't mean to
seriously suggest using a regex merely to split on whitespace?
>>> import timeit
>>> timeit.Timer("s.split()", "s = 'one two three four'").repeat()
[1.4074358940124512, 1.3505148887634277, 1.3469438552856445]
>>> timeit.Timer("re.split('\s+', s)", "import re;s = 'one two
three four'").repeat()
[7.9205508232116699, 7.8833441734313965, 7.9301259517669678]
--
Steven
More information about the Python-list
mailing list