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

Discussion in 'Python' started by Massi, May 13, 2012.

  1. Massi

    Massi Guest

    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!
     
    Massi, May 13, 2012
    #1
    1. Advertising

  2. Re: Yet another "split string by spaces preserving single quotes"problem

    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
     
    Steven D'Aprano, May 15, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas

    Yet another modelsim problem

    Thomas, Aug 12, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,330
    K Mussatt
    Aug 14, 2003
  2. Replies:
    6
    Views:
    5,342
    Alan Moore
    May 24, 2005
  3. Will Spencer

    Yet another problem with Netscape

    Will Spencer, Jul 24, 2004, in forum: HTML
    Replies:
    28
    Views:
    859
    Matrix
    Jul 26, 2004
  4. Si
    Replies:
    22
    Views:
    821
    Toby Inkster
    Feb 16, 2005
  5. Berehem
    Replies:
    4
    Views:
    568
    Lawrence Kirby
    Apr 28, 2005
Loading...

Share This Page