String splitting by spaces question

M

Massi

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

Alemu Tadesse

Hi Everyone,

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

Alemu


-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On Behalf
Of Massi
Sent: Wednesday, November 23, 2011 10:10 AM
To: (e-mail address removed)
Subject: String splitting by spaces question

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

Arnaud Delobelle

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']
 
N

Nick Dokos

Alemu Tadesse said:
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
 
P

Phil Rist

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>
 
D

DevPlayer

This is an 'example string'

Don't for get to watch for things like:

Don't, Can't, Won't, I'll, He'll, Hor'davors, Mc'Kinly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top