Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Yet another "split string by spaces preserving single quotes" problem

Reply
Thread Tools

Yet another "split string by spaces preserving single quotes" problem

 
 
Massi
Guest
Posts: n/a
 
      05-13-2012
Hi everyone,
I know this question has been asked thousands of times, but in my case
I have an additional requirement to be satisfied. I need to handle
substrings in the form 'string with spaces':'another string with
spaces' as a single token; I mean, if I have this string:

s ="This is a 'simple test':'string which' shows 'exactly my'
problem"

I need to split it as follow (the single quotes must be mantained in
the splitted list):

["This", "is", "a", "'simple test':'string which'", "shows",
"'exactly my'", "problem"]

Up to know I have written some ugly code which uses regular
expression:

splitter = re.compile("(?=\s|^)('[^']+') | ('[^']+')(?=\s|$)")

temp = [t for t in splitter.split(s) if t not in [None, '']]
print temp
t = []
for i, p in enumerate(temp) :
for x in ([p] if (p[0] == "'" and p[1] == "'") else p.split('
')) :
t.append(x)

But it does not handle "colon" case.
Any hints? Thanks in advance!
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-15-2012
On Sun, 13 May 2012 14:14:58 -0700, Massi wrote:

> Hi everyone,
> I know this question has been asked thousands of times, but in my case I
> have an additional requirement to be satisfied. I need to handle
> substrings in the form 'string with spaces':'another string with spaces'
> as a single token; I mean, if I have this string:
>
> s ="This is a 'simple test':'string which' shows 'exactly my' problem"
>
> I need to split it as follow (the single quotes must be mantained in the
> splitted list):
>
> ["This", "is", "a", "'simple test':'string which'", "shows", "'exactly
> my'", "problem"]
>
> Up to know I have written some ugly code which uses regular expression:


And now you have two problems *wink*


> Any hints? Thanks in advance!


>>> s = "This is a 'simple test':'string which' shows 'exactly my'

problem"
>>> import shlex
>>> result = shlex.split(s, posix=True)
>>> result

['This', 'is', 'a', 'simple test:string which', 'shows', 'exactly my',
'problem']


Then do some post-processing on the result:

>>> ["'"+s+"'" if " " in s else s for s in result]

['This', 'is', 'a', "'simple test:string which'", 'shows', "'exactly
my'", 'problem']


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
Sign preserving Vs value preserving sophia.agnes@gmail.com C Programming 4 12-07-2007 03:14 PM
How can I search and replace a string while preserving (not removing) trailing spaces? rsarpi Perl Misc 1 05-13-2007 04:29 AM
integral promotion, arithmetic conversion, value preserving, unsigned preserving??? TTroy C Programming 16 01-31-2005 10:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57