split string saving screened spaces

S

Sergey

Which module to use to do such thing:

"-a -b -c '1 2 3'" -> ["-a", "-b", "-c", "'1 2 3'"]

(i have string, need to pass it to getopt)
 
B

bonono

Sergey said:
Which module to use to do such thing:

"-a -b -c '1 2 3'" -> ["-a", "-b", "-c", "'1 2 3'"]

(i have string, need to pass it to getopt)
seems like CSV except that the seperator is space. You may check to see
if the CSV module can take space as delimiter.
 
P

Paul McGuire

Pyparsing has built-in quoted string support.


from pyparsing import OneOrMore,Word,alphanums,quotedString

item = Word('-',alphanums) | quotedString | Word(alphanums)
items = OneOrMore(item)

print items.parseString( "-a -b -c '1 2 3' -d 5 -e zork2000" )

gives:
['-a', '-b', '-c', "'1 2 3'", '-d', '5', '-e', 'zork2000']


Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top