I have to parse an input string in python and extract certain parts from it.
the format of the string is
(xx,yyy,(aa,bb,...)) // Inner parenthesis can hold one or more characters in it
I want a function to return xx, yyyy and a list containing aa, bb … etc
I can ofcourse do it by trying to split of the parenthesis and stuff but I want to know if there a proper pythonic way of extracting such info from a string
I have this code which works, but is there a better way to do it (without regex)
def processInput(inputStr):
value = inputStr.strip()[1:-1]
parts = value.split(',', 2)
return parts[0], parts[1], (parts[2].strip()[1:-1]).split(',')
If you’re allergic to REs, you could use pyparsing:
pyparsingalso makes it easy to control the exact form of results (e.g. by grouping the last 3 items into their own sublist), if you want. But I think the nicest aspect is how natural (depending on how much space you want to devote to it) you can make the “grammar specification” read: an open paren, a word, a comma, a word, a comma, an open paren, a delimited list of words, two closed parentheses (if you find the assignment tosabove not so easy to read, I guess it’s my fault for not choosing longer identifiers;-).