Parsing Strings in Enclosed in Curly Braces

Peter Otten __peter__ at web.de
Fri May 15 16:01:18 EDT 2009


kay at fiber-space.de wrote:

> 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.

Keep one of the splitting characters to avoid that (theoretical) risk:

ttable = string.maketrans("{} ", "{{{")

Peter




More information about the Python-list mailing list