Prev: Miracles of the devil and beat of the revolution of religious reform
Next: Advice Criticism on Python App
From: Emile van Sebille on 23 Mar 2010 18:34 On 3/23/2010 3:09 PM python(a)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, save it, surround the > remaining text with html tags, and then add back the leading and > trailing whitespace. I'd do it this way: target = ' spam and eggs ' stripped = target.strip() replaced = target.replace(stripped,"<html>%s</html>" % stripped) HTH, Emile > > The only solution I can think of is regex, and that makes me > think of the 2 proverbial problems that come with that :) > > Is there a 'better' solution than regex for this scenario? (Seems > like this would be a common type of string processing). > > Thanks, > Malcolm > >
From: python on 23 Mar 2010 18:45 Emile, > target = ' spam and eggs ' > stripped = target.strip() > replaced = target.replace(stripped,"<html>%s</html>" % stripped) Brilliant! That's just the type of clever solution I was looking for. Thank you! Malcolm ----- Original message ----- From: "Emile van Sebille" <emile(a)fenx.com> To: python-list(a)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 python(a)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, save it, surround the > remaining text with html tags, and then add back the leading and > trailing whitespace. I'd do it this way: target = ' spam and eggs ' stripped = target.strip() replaced = target.replace(stripped,"<html>%s</html>" % stripped) HTH, Emile > > The only solution I can think of is regex, and that makes me > think of the 2 proverbial problems that come with that :) > > Is there a 'better' solution than regex for this scenario? (Seems > like this would be a common type of string processing). > > Thanks, > Malcolm > > -- http://mail.python.org/mailman/listinfo/python-list
From: Tim Chase on 23 Mar 2010 19:38 python(a)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 > remaining text with html tags, and then add back the leading and > trailing whitespace. > > The only solution I can think of is regex, and that makes me > think of the 2 proverbial problems that come with that :) 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() While Emile's solution works nicely for your particular use-case, in the event you need to discern between leading/trailing whitespace, the above makes it pretty easy. -tkc
From: Martin P. Hellwig on 23 Mar 2010 19:57
On 03/23/10 23:38, Tim Chase wrote: <cut> > 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() <cut> Ahhh regex, the hammer, Swiss Army Knife, cable tie, duct tape, superglue and soldering iron, all wrapped in one*. Mastery of it will enable you to drive the nails in your coffin at an incomparable speed. :-) *Yes I was a sysadmin, why do you ask? -- mph |