On Jul 31, 3:30 pm, Helmut Jarausch <[EMAIL PROTECTED]> wrote: > 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') >
Pyparsing has built-in support for special treatment of quoted strings. Observe: from pyparsing import * data = r'abc "xy z" "1 2 3" "a \" x"' quotedString.setParseAction(removeQuotes) print OneOrMore(quotedString | Word(printables) ).parseString(data) prints: ['abc', 'xy z', '1 2 3', 'a \\" x'] Or perhaps a bit trickier, do the same while skipping items inside /* */ comments: data = r'abc /* 456 "xy z" */ "1 2 3" "a \" x"' quotedString.setParseAction(removeQuotes) print OneOrMore(quotedString | Word(printables) ) \ .ignore(cStyleComment).parseString(data) prints: ['abc', '1 2 3', 'a \\" x'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list