Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16119

Re: String splitting by spaces question

References <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@r9g2000vbw.googlegroups.com>
Date 2011-11-23 17:40 +0000
Subject Re: String splitting by spaces question
From Arnaud Delobelle <arnodel@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2976.1322070040.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 23 November 2011 17:10, Massi <massi_srb@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

String splitting  by spaces question Massi <massi_srb@msn.com> - 2011-11-23 09:10 -0800
  RE: String splitting  by spaces question "Alemu Tadesse" <atadesse@sunedison.com> - 2011-11-23 11:31 -0600
  Re: String splitting by spaces question Arnaud Delobelle <arnodel@gmail.com> - 2011-11-23 17:40 +0000
  Re: String splitting by spaces question Nick Dokos <nicholas.dokos@hp.com> - 2011-11-23 12:51 -0500
  Re: String splitting  by spaces question Miki Tebeka <miki.tebeka@gmail.com> - 2011-11-23 12:40 -0800
  Re: String splitting  by spaces question Phil Rist <Phil_member@newsguy.com> - 2011-11-23 16:20 -0800
    Re: String splitting by spaces question DevPlayer <devplayer@gmail.com> - 2011-11-23 19:53 -0800

csiph-web