On 03/23/10 23:38, Tim Chase wrote:
Just in case you're okay with a regexp solution, you can use
>>> s = "\t\tabc def "
>>> import re
>>> r = re.compile(r'^(\s*)(.*?)(\s*)$')
>>> m = re.match(s)
>>> m.groups()
('\t\t', 'abc def', ' ')
>>> leading, text, trailing = m.groups()
Ahhh regex,
pyt...@bdurham.com wrote:
I'm looking for a pythonic way to trim and keep leading
whitespace in a string.
Use case: I have a bunch of text strings with various amounts of
leading and trailing whitespace (spaces and tabs). I want to grab
the leading and trailing whitespace, save it, surround the
regex is not goto that you should always avoid using it. It have its own
use-case, here regex solution is intuitive although @emile have done this
for you via string manpulation.
On Wed, Mar 24, 2010 at 4:09 AM, Daniel Chiquito
wrote:
> As far as I know, I don't think there is anything that strip
uot;Emile van Sebille"
To: python-list@python.org
Date: Tue, 23 Mar 2010 15:34:48 -0700
Subject: Re: Pythonic way to trim and keep leading and trailing
whitespace
On 3/23/2010 3:09 PM pyt...@bdurham.com said...
> I'm looking for a pythonic way to trim and keep leading
> whitespace in
As far as I know, I don't think there is anything that strips it and returns
the material that was stripped. Regex's would be your best bet.
Daniel
On Tue, Mar 23, 2010 at 6:09 PM, wrote:
> I'm looking for a pythonic way to trim and keep leading whitespace in a
> string.
>
> Use case: I have a
On 3/23/2010 3:09 PM pyt...@bdurham.com said...
I'm looking for a pythonic way to trim and keep leading
whitespace in a string.
Use case: I have a bunch of text strings with various amounts of
leading and trailing whitespace (spaces and tabs). I want to grab
the leading and trailing whitespace,
I'm looking for a pythonic way to trim and keep leading
whitespace in a string.
Use case: I have a bunch of text strings with various amounts of
leading and trailing whitespace (spaces and tabs). I want to grab
the leading and trailing whitespace, save it, surround the
remaining text with html tag