Re: String splitting by spaces question

2011-11-23 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String splitting by spaces question

2011-11-23 Thread Phil Rist
In article <3f19e4c0-e010-4cb2-9f71-dd09e0d3c...@r9g2000vbw.googlegroups.com>,
Massi says...
>
>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>

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String splitting by spaces question

2011-11-23 Thread Miki Tebeka
http://docs.python.org/library/shlex.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String splitting by spaces question

2011-11-23 Thread Jerry Hill
On Wed, Nov 23, 2011 at 12:10 PM, Massi  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:
>
> ["This", "is", "an", "example string"]
>
> Which is the best way to achieve this?
>


This sounds a lot like the way a shell parses arguments on the command
line.  If that's your desire, python has a module in the standard library
that will help, called shlex (http://docs.python.org/library/shlex.html).
Particularly, shlex.split may do exactly what you want out of the box:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit
(Intel)] on win32

>>> import shlex
>>> s = "This is an 'example string'"
>>> shlex.split(s)
['This', 'is', 'an', 'example string']
>>>

-- 
Jerry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String splitting by spaces question

2011-11-23 Thread Nick Dokos
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String splitting by spaces question

2011-11-23 Thread Arnaud Delobelle
On 23 November 2011 17:10, Massi  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
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: String splitting by spaces question

2011-11-23 Thread 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: python-list-bounces+atadesse=sunedison@python.org
[mailto:python-list-bounces+atadesse=sunedison@python.org] On Behalf
Of Massi
Sent: Wednesday, November 23, 2011 10:10 AM
To: python-list@python.org
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!
-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


String splitting by spaces question

2011-11-23 Thread 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!
-- 
http://mail.python.org/mailman/listinfo/python-list