“Deparing”使用 pyparsing 的列表
是否可以给 pyparsing 一个解析列表并让它返回原始字符串?
Is it possible to give pyparsing a parsed list and have it return the original string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您已指示解析器不要丢弃任何输入,则可以。您可以使用
Combine
组合器来完成此操作。假设您的输入是:
这是一个解析器,它获取列表的确切文本:
“deparse”:
它为您提供初始输入。
但这是有代价的:将所有额外的空格作为标记浮动通常并不方便,而且您会注意到我们必须在
myList
中显式关闭空格跳过关闭 >。这是一个删除空格的版本:请注意,此时您还没有得到文字输入,但这对您来说可能已经足够了。另请注意,我们必须明确告诉合并允许跳过空格。
但实际上,在许多情况下您甚至不关心分隔符;您希望解析器专注于项目本身。有一个名为
commaSeparatedList
的函数,可以方便地为您去除分隔符和空格:不过,在这种情况下,“解析”步骤没有足够的信息来使重构的字符串有意义:
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: