How to Split a String

imho certo at comeno.it
Thu Nov 29 23:07:54 CET 2007


Grant Edwards ha scritto:

>> 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']
> 

Uhm, agree. I supposed quoted strings were presumed to be 'trivial'.
Definitely csv module is the solution :-)



More information about the Python-list mailing list