How to Split a String
Grant Edwards
grante at visi.com
Thu Nov 29 16:07:01 EST 2007
On 2007-11-29, imho <certo at comeno.it> wrote:
> Siah ha scritto:
>> Hi,
>>
>> 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.
>>
>> Please help me with this,
>> Thanks,
>> Sia
>
> One solution:
>
> >>> s = '(a, b, "c", d, "e")'
> >>> print [x.strip('" ') for x in s.strip('()').split(',')]
> ['a', 'b', 'c', 'd', 'e']
That fails when a quoted string contains commas:
>>> s = '(a, b, "c", d, "e,f,g")'
>>> print [x.strip('" ') for x in s.strip('()').split(',')]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
I presume the correct result would be
['a', 'b', 'c', 'd', 'e,f,g']
--
Grant Edwards grante Yow! I wonder if I could
at ever get started in the
visi.com credit world?
More information about the Python-list
mailing list