[Tutor] manipulating a string

Mark Tolonen metolone+gmane at gmail.com
Tue Jul 15 10:06:41 CEST 2008


"Bryan Fodness" <bryan.fodness at gmail.com> wrote in message 
news:fbf64d2b0807141541o7f462e3x17cd6c7dd8b752b4 at mail.gmail.com...
> I have a string (column names) that I need to split.
>
> D_H = 'D 5 10 15 20 25 30 35 40 D Upper D Lower'
>
> I cannot do a simple list(D_H).split because the last two strings have a 
> space between them (D Upper and D Lower are two, not four labels).
>
> Any suggestions?

How was the string built?  Is it possible to delimit the columns with 
something besides a space, since a space is a valid character in a column 
name.  Baring that, adding a little knowledge of the string is needed:

>>> import re
>>> D_H = 'D 5 10 15 20 25 30 35 40 D Upper D Lower'
>>> print re.split(r'\s(?![UL])',D_H) # split on spaces not followed by U or 
>>> L.
['D', '5', '10', '15', '20', '25', '30', '35', '40', 'D Upper', 'D Lower']

--
Mark




More information about the Tutor mailing list