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


Groups > comp.lang.python > #16120

Re: String splitting by spaces question

From Nick Dokos <nicholas.dokos@hp.com>
Subject Re: String splitting by spaces question
References <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@r9g2000vbw.googlegroups.com> <6FA6EB75E3CA4744ADF9EF97D8DAC18B01967924@mail.sunedison.com>
Organization HPCS
Date 2011-11-23 12:51 -0500
Newsgroups comp.lang.python
Message-ID <mailman.2977.1322071012.27778.python-list@python.org> (permalink)

Show all headers | View raw


Alemu Tadesse <atadesse@sunedison.com> wrote:

> Can we use rsplit function on an array or vector of strings ? it works
> for one not for vector

> ...
> 
> 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!

You can use a list comprehension:

l2 = [x.rsplit(...) for x in l]

But for the original question, maybe the csv module would be
more useful: you can change delimiters and quotechars to match
your input:

    import csv
    reader = csv.reader(open("foo.txt", "rb"), delimiter=' ', quotechar="'")
    for row in reader:
        print row

Nick

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