Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'comprises': 0.07; 'advance!': 0.09; 'csv': 0.09; 'delimiters': 0.16; 'nick': 0.16; 'row': 0.16; 'subject:String': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.17; 'wrote:': 0.18; 'this?': 0.19; 'cc:no real name:2**0': 0.20; 'maybe': 0.21; 'header:In-Reply-To:1': 0.22; 'string': 0.24; 'module': 0.26; 'function': 0.27; 'skip:[ 10': 0.27; 'import': 0.27; 'pass': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'array': 0.30; 'splitting': 0.30; 'thanks': 0.31; 'list': 0.32; 'match': 0.34; 'question,': 0.34; 'parse': 0.34; '...': 0.36; 'cc:2**1': 0.36; 'but': 0.37; 'received:org': 0.38; 'more': 0.61; 'achieve': 0.61; 'your': 0.61; 'reply-to:no real name:2**0': 0.72; 'header:Reply-to:1': 0.84; 'cc:addr:msn.com': 0.84; 'quotations': 0.84; 'spaces.': 0.84; 'substrings': 0.84 To: "Alemu Tadesse" From: Nick Dokos Subject: Re: String splitting by spaces question In-Reply-To: Message from "Alemu Tadesse" of "Wed, 23 Nov 2011 11:31:21 CST." <6FA6EB75E3CA4744ADF9EF97D8DAC18B01967924@mail.sunedison.com> References: <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@r9g2000vbw.googlegroups.com> <6FA6EB75E3CA4744ADF9EF97D8DAC18B01967924@mail.sunedison.com> Organization: HPCS X-Mailer: MH-E 8.3; nmh 1.3; GNU Emacs 24.0.90 Date: Wed, 23 Nov 2011 12:51:12 -0500 Sender: nick@dokosmarshall.org Cc: python-list@python.org, Massi X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: nicholas.dokos@hp.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322071012 news.xs4all.nl 6892 [2001:888:2000:d::a6]:38416 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16120 Alemu Tadesse 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