本文介绍了PyParsing:是否有可能全局抑制所有文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据集,可使用以下行进行解析:

I have a simple data set to parse with lines like the following:

R1 (a/30) to R2 (b/30), metric 30

我上面需要的唯一数据如下:

The only data that I need from the above is as follows:

R1, a, 30, R2, 192.168.0.2, 30, 30

我可以使用pyparsing轻松解析所有这些内容,但是我要么在输出中得到一堆字面量,要么必须在解析语法中明确说出Literal(thing).suppress(),这很烦人.

I can parse all of this easily with pyparsing, but I either end up with a bunch of literals in my output, or I have to specifically say Literal(thing).suppress() in my parsing grammar, which gets tiresome.

理想情况下,我想为上述代码编写语法:

Ideally, I'd like to write a grammar for the above like:

Word(alphanums) + '(' + Word(alphanums) + '/' + Word(nums) + ... etc.

并让文字标记被忽略.我可以说.suppressAllLiterals()之类的话吗?

and have the literal tokens get ignored. Can I say anything like .suppressAllLiterals()?

注意:

  • PyParsing的新手
  • 我已经阅读了文档和5或6个示例
  • 搜索过Google

谢谢!

推荐答案

您可以在ParserElement上使用此方法-导入pyparsing后立即调用该方法:

You can use this method on ParserElement - call it immediately after importing pyparsing:

from pyparsing import ...whatever...
ParserElement.inlineLiteralsUsing(Suppress)

现在,解析器中的所有字符串文字将被包装在Suppress对象中,并被排除在结果之外,而不是默认的Literal.

Now all the string literals in your parser will be wrapped in Suppress objects, and left out of the results, rather than the default Literal.

(有一天,当我可以打破向后兼容性时,我可能会将其设置为v3.0中的默认值.)

(I will probably make this the default in v3.0, someday, when I can break backward compatibility.)

这篇关于PyParsing:是否有可能全局抑制所有文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 10:56