Parsing Strings in Enclosed in Curly Braces

kay at fiber-space.de kay at fiber-space.de
Fri May 15 21:31:36 CEST 2009


On 15 Mai, 18:12, xama... at yahoo.com wrote:
> How do you parse a string enclosed in Curly Braces?
>
> For instance:
>
> x = "{ABC EFG IJK LMN OPQ}"
>
> I want to do x.split('{} ') and it does not work. Why does it not work
> and what are EXCEPTIONS to using the split method?
>
> That I want to split based on '{', '}' and WHITESPACE.
>
> Please advise.
>
> Regards,
> Xav

import string
ttable   = string.maketrans("{} ", "\1\1\1")
print x.translate(ttable).split("\1")   # -> ['', 'ABC', 'EFG', 'IJK',
'LMN', 'OPQ', '']

The validity of the translation+split depends on the presence of \1 in
the original string of course.




More information about the Python-list mailing list