Re: split a string of space separated substrings - elegant solution?



On Tue, 2007-07-31 at 22:30 +0200, Helmut Jarausch wrote:
Hi,

I'm looking for an elegant solution to the following (quite common)
problem:

Given a string of substrings separated by white space,
split this into tuple/list of elements.
The problem are quoted substrings like

abc "xy z" "1 2 3" "a \" x"

should be split into ('abc','xy z','1 2 3','a " x')

import shlex
shlex.split('abc "xy z" "1 2 3" "a \\" x"')
['abc', 'xy z', '1 2 3', 'a " x']

I hope that's elegant enough ;)

--
Carsten Haese
http://informixdb.sourceforge.net


.



Relevant Pages

  • Re: split a string of space separated substrings - elegant solution?
    ... I'm looking for an elegant solution to the following (quite common) ... Given a string of substrings separated by white space, ...
    (comp.lang.python)
  • Re: Need help with IF function
    ... There maybe a more elegant solution but meantime, ... The problem is that column C is a long string of text that may or may ... not include the word German anywhere in the text string. ... The second formula will return a blank if the word "English" is found ...
    (microsoft.public.excel)
  • enum
    ... function that returns a textual representation (i.e. a string). ... Writing a large switch construct is surely a solution, ... Is there a better and more elegant solution? ...
    (alt.comp.lang.learn.c-cpp)
  • split a string of space separated substrings - elegant solution?
    ... I'm looking for an elegant solution to the following ... one probably has to protect white space between ... RWTH - Aachen University ...
    (comp.lang.python)
  • Re: String spliting and inclusion
    ... have a long string and I want to split into smaller ... strings no longer than 50 characters in length. ... max of 50 characters without breaking up words. ... might lead to the most elegant solution: ...
    (comp.lang.ruby)