String splitting by spaces question

Arnaud Delobelle arnodel at gmail.com
Wed Nov 23 12:40:37 EST 2011


On 23 November 2011 17:10, Massi <massi_srb at msn.com> wrote:
> Hi everyone,
>> I have to parse a string and splitting it by spaces. The problem is
> that the string can include substrings comprises by quotations which
> must mantain the spaces. What I need is to pass from a string like:
>> This is an 'example string'
>> to the following vector:

You mean "list"
> ["This", "is", "an", "example string"]
>
Here's a way:
>>> s = "This is an 'example string' with 'quotes again'"
>>> [x for i, p in enumerate(s.split("'")) for x in ([p] if i%2 else p.split())]
['This', 'is', 'an', 'example string', 'with', 'quotes again']
-- 
Arnaud


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /