Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16114 > unrolled thread
| Started by | Massi <massi_srb@msn.com> |
|---|---|
| First post | 2011-11-23 09:10 -0800 |
| Last post | 2011-11-23 19:53 -0800 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Massi <massi_srb@msn.com> |
|---|---|
| Date | 2011-11-23 09:10 -0800 |
| Subject | String splitting by spaces question |
| Message-ID | <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@r9g2000vbw.googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | "Alemu Tadesse" <atadesse@sunedison.com> |
|---|---|
| Date | 2011-11-23 11:31 -0600 |
| Message-ID | <mailman.2975.1322069528.27778.python-list@python.org> |
| In reply to | #16114 |
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.com@python.org [mailto:python-list-bounces+atadesse=sunedison.com@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
[toc] | [prev] | [next] | [standalone]
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2011-11-23 17:40 +0000 |
| Subject | Re: String splitting by spaces question |
| Message-ID | <mailman.2976.1322070040.27778.python-list@python.org> |
| In reply to | #16114 |
On 23 November 2011 17:10, Massi <massi_srb@msn.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Nick Dokos <nicholas.dokos@hp.com> |
|---|---|
| Date | 2011-11-23 12:51 -0500 |
| Subject | Re: String splitting by spaces question |
| Message-ID | <mailman.2977.1322071012.27778.python-list@python.org> |
| In reply to | #16114 |
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
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-11-23 12:40 -0800 |
| Message-ID | <29337570.208.1322080827452.JavaMail.geo-discussion-forums@yqiv14> |
| In reply to | #16114 |
http://docs.python.org/library/shlex.html
[toc] | [prev] | [next] | [standalone]
| From | Phil Rist <Phil_member@newsguy.com> |
|---|---|
| Date | 2011-11-23 16:20 -0800 |
| Message-ID | <jak2kr09dh@drn.newsguy.com> |
| In reply to | #16114 |
In article <3f19e4c0-e010-4cb2-9f71-dd09e0d3cb1f@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>
[toc] | [prev] | [next] | [standalone]
| From | DevPlayer <devplayer@gmail.com> |
|---|---|
| Date | 2011-11-23 19:53 -0800 |
| Subject | Re: String splitting by spaces question |
| Message-ID | <45795de6-01d4-4969-9a6e-872ceadaf645@da3g2000vbb.googlegroups.com> |
| In reply to | #16134 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web