gry@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 -- http://mail.python.org/mailman/listinfo/python-list