How to Split a String

Siah siasookhteh at gmail.com
Thu Nov 29 15:33:26 EST 2007


The basic split/strip method wouldn't split '(a, b, "c,...", d)',
which is why I chose not to use it.

The csv solution seems to work well, that helped me much here (thank
you), but I am looking to see if I can get it solved with some regular
expression. This is how far I've come so far, but it needs much work:
re.compile('"*,"*').split(x[1:-1])

Thanks for your help,
Sia




On Nov 29, 3:24 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n... at spamgourmet.com> wrote:
> 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