regexp for sequence of quoted strings

Magnus Lycka lycka at carmen.se
Mon May 30 05:23:07 EDT 2005


gry at ll.mit.edu wrote:
> I have a string like:
>  {'the','dog\'s','bite'}
> or maybe:
>  {'the'}
> or sometimes:
>  {}
...
> I want to end up with a python array of strings like:
> 
> ['the', "dog's", 'bite']

Assuming that you trust the input, you could always use eval,
but since it seems fairly easy to solve anyway, that might
not be the best (at least not safest) solution.

 >>> strings = [r'''{'the','dog\'s','bite'}''', '''{'the'}''', '''{}''']
 >>> for s in strings:
...     print eval('['+s[1:-1]+']')
...
['the', "dog's", 'bite']
['the']
[]



More information about the Python-list mailing list