How to use IndentedBlock correctly #430
-
Greetings! I am using version 3.0.9, and am trying to use Here is my code:
And here is the output (the first line shows how I want the output; the second line shows the undesirable output where a non-indented word has matched successfully):
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 more so that your code could actually parse all lines. Why your code did not work was because A: it was forcing pyparsing to find and indent block even if it was not there, B: it only parsed one directory. Warning: either my code is not completly right or there is a bug with pyparsing but it seems to be misinterpreting things as indented. This is meant to help you find your answer, not be your answer, so if you get this issue ill leave you to fix it. Hope this helps! |
Beta Was this translation helpful? Give feedback.
This is a late response, but if you are still interested, here is the sultion:
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…