A nice way to parse a string that follows some grammar rules is the 3rd party pyparsing library. This is very generic lacking a formal grammar definition of allowed user input:
#coding:utf8
from pyparsing import *
# Names for symbols
_lparen = Suppress('(')
_rparen = Suppress(')')
_quote = Suppress('"')
_eq = Suppress('=')
# Parsing grammar definition
data = (_lparen + # left parenthesis
delimitedList( # Zero or more comma-separated items
Group( # Group the contained unsuppressed tokens in a list
Regex(u'[^=,)\s]+') + # Grab everything up to an equal, comma, endparen or whitespace as a token
Optional( # Optionally...
_eq + # match an =
_quote + # a quote
Regex(u'[^"]*') + # Grab everything up to another quote as a token
_quote) # a quote
) # EndGroup - will have one or two items.
) + # EndList
_rparen) # right parenthesis
def process(s):
items = data.parseString(s).asList()
args = [i[0] for i in items if len(i) == 1]
kwargs = {i[0]:i[1] for i in items if len(i) == 2}
return args,kwargs
s = u'(鞋子="AAA", last = "BBB", abcd)'
args,kwargs = process(s)
for a in args:
print a
for k,v in kwargs.items():
print k,v
Output:
abcd
鞋子 AAA
last BBB