Skip to content Skip to sidebar Skip to footer

Difficulty Of This Particular Job Using Pyparsing? (beginner)

I have a task to do that I'm sure Python and pyparsing could really help with, but I'm still too much of a novice with programming to make a smart choice about how challenging the

Solution 1:

In pyparsing, your example would look something like:

from pyparsing import Word,alphanums,Forward,Optional,nestedExpr,delimitedList

topicString = Word(alphanums+'-')
expr = Forward()
expr << topicString + Optional(nestedExpr(content=delimitedList(expr)))

test = 'topic(subtopic(sub-subtopic), subtopic2), topic2'print delimitedList(expr).parseString(test).asList()

Prints

['topic', ['subtopic', ['sub-subtopic'], 'subtopic2'], 'topic2']

Converting to topic:subtopic, etc. is left as an exercise for the OP.

Post a Comment for "Difficulty Of This Particular Job Using Pyparsing? (beginner)"