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



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 csv

s = 'abc "xy z" "1 2 3" "a \\" x"'
r = iter(csv.reader([s], delimiter=" ", escapechar="\\"))
print r.next()

w.
.



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)
  • PRE-PEP: new Path class
    ... Should path be a subclass of str? ... be times a developer wants to use string methods, ... the most common operations should be supported directly. ... to divide (separate) things. ...
    (comp.lang.python)
  • Re: Email Merge in Word
    ... Different software packages can treat "null", "a string set to ''", and "a string containing white space differently, and may also treat variable-length and fixed-length data differently in this respect. ... Also, when I set up my query, I selected the criteria for the email address field "is not null", and there are still some blank records with no emails. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Email Merge in Word
    ... WHERE mystring is not null AND trim'' ... If the address looks blank but len) is> 0 then the field probably contains invisible non-space characters. ... Different software packages can treat "null", "a string set to ''", and "a string containing white space differently, and may also treat variable-length and fixed-length data differently in this respect. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Is there a Function and Function Argument generic self-reference?
    ... Private Class Common ... Public Overridable Function As String ... Sub Stored_Procedure_Common(ByVal Command As DB2Command) ...
    (microsoft.public.dotnet.languages.vb)