Skip to content

How to use IndentedBlock correctly #430

Answered by thatrandomperson5
chaihub asked this question in Q&A
Discussion options

You must be logged in to vote

This is a late response, but if you are still interested, here is the sultion:

from pyparsing import *

DATA1 = """
Directory1
    Filea
    Fileb
    Filec
Directory2
Directory3
"""

DATA2 = """
Directory1
Directory2
    Filea
    Fileb
    Filec
Directory3
"""

dirData = Word(alphanums)
fileData = Word(alphas)
dirListing = OneOrMore(dirData+(IndentedBlock(fileData) | Suppress(StringEnd())))

parseTree = dirListing.parse_string(DATA1)
parseTree.pprint()
parseTree = dirListing.parse_string(DATA2)
parseTree.pprint()

I added an indent block match-first with a suppressed stringEnd to make sure that there were no glitches about where lines ended ( I got some of those). I also added one or mor…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@chaihub
Comment options

Answer selected by chaihub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants