regexp for sequence of quoted strings
Steven Bethard
steven.bethard at gmail.com
Wed May 25 16:11:42 EDT 2005
gry at ll.mit.edu wrote:
> I have a string like:
> {'the','dog\'s','bite'}
> or maybe:
> {'the'}
> or sometimes:
> {}
>
[snip]
>
> I want to end up with a python array of strings like:
>
> ['the', "dog's", 'bite']
>
> Any simple clear way of parsing this in python would be
> great; I just assume that "re" is the appropriate technique.
> Performance is not an issue.
py> s = "{'the','dog\'s','bite'}"
py> s
"{'the','dog's','bite'}"
py> s[1:-1]
"'the','dog's','bite'"
py> s[1:-1].split(',')
["'the'", "'dog's'", "'bite'"]
py> [item[1:-1] for item in s[1:-1].split(',')]
['the', "dog's", 'bite']
py> s = "{'the'}"
py> [item[1:-1] for item in s[1:-1].split(',')]
['the']
py> s = "{}"
py> [item[1:-1] for item in s[1:-1].split(',')]
['']
Not sure what you want in the last case, but if you want an empty list,
you can probably add a simple if-statement to check if s[1:-1] is non-empty.
HTH,
STeVe
More information about the Python-list
mailing list