String splitting by spaces question

Phil Rist Phil_member at newsguy.com
Wed Nov 23 19:20:43 EST 2011


In article <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f at r9g2000vbw.googlegroups.com>,
Massi says...
>
>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:
>
>["This", "is", "an", "example string"]
>
>Which is the best way to achieve this?
>Thanks in advance!


Is this what you want?

import shlex


lText = "This is a 'short string' for you to read."
lWords = shlex.split(lText)
print lWords

produces,

['This', 'is', 'a', 'short string', 'for', 'you', 'to', 'read.']

Shlex can be found under 'Program Frameworks' under 'The Python Standard
Library' of ActivePython 2.7 documentation.

C:\Source\Python\New>




More information about the Python-list mailing list