String splitting by spaces question
Nick Dokos
nicholas.dokos at hp.com
Wed Nov 23 12:51:12 EST 2011
Alemu Tadesse <atadesse at 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
More information about the Python-list
mailing list