How to Split a String

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Thu Nov 29 21:24:04 CET 2007


Siah wrote:

> I need to convert the string: '(a, b, "c", d, "e")' into the
> following list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader
> does. I usually use the split function, but this mini-monster
> wouldn't properly get split up due to those random quotations
> postgresql returns to me.

I heavily suggest you to look at the docs -- those are very basic
functions.

http://docs.python.org/lib/string-methods.html

One solution might be:

>>> results = []
>>> for part in '(a, b, "c", d, "e")'.split(","):
...  part = part.strip()
...  part = part.strip("(),\"")
...  part = part.strip()
...  results.append(part)
... 
>>> print results
['a', 'b', 'c', 'd', 'e']
>>> 

Regards,


Björn


-- 
BOFH excuse #285:

Telecommunications is upgrading. 




More information about the Python-list mailing list