Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!drn From: Phil Rist Newsgroups: comp.lang.python Subject: Re: String splitting by spaces question Date: 23 Nov 2011 16:20:43 -0800 Organization: NewsGuy - Unlimited Usenet $19.95 Lines: 37 Message-ID: References: <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@r9g2000vbw.googlegroups.com> NNTP-Posting-Host: p21be65762de3bafd7ceb2d37de6a66d665d3091116fb9d91.newsdawg.com User-Agent: Direct Read News 5.60 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16134 In article <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@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>