Is it possible to give pyparsing a parsed list and have it return the original string?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Yes, you can if you've instructed the parser not to throw away any input. You do it with the
Combine
combinator.Let's say your input is:
Here's a parser that grabs the exact text of the list:
To "deparse":
which gives you the initial input back.
But this comes at a cost: having all that extra whitespace floating around as tokens is usually not convenient, and you'll note that we had to explicitly turn whitespace skipping off in
myList
. Here's a version that strips whitespace:Note you're not getting the literal input back at this point, but this may be good enough for you. Also note we had to explicitly tell Combine to allow the skipping of whitespace.
Really, though, in many cases you don't even care about the delimiters; you want the parser to focus on the items themselves. There's a function called
commaSeparatedList
that conveniently strips both delimiters and whitespace for you:In this case, though, the "deparsing" step doesn't have enough information for the reconstituted string to make sense: