python - 如果组件解析器有结果名称,为什么结果元素是 ParseResults 类型?
问题描述
我不清楚为什么解析结果列表有时包含 ParseResults 元素而不是字符串。
例如:
from pyparsing import *
text = "pfx01ABC"
# nested result name
pfx = Literal("pfx")("prefix")
combined = Combine(pfx + Word(alphanums))("combined")
combined.runTests(text)
res = combined.parseString(text)
print("type(res[0]):", type(res[0]))
print('type(res["combined"]):', type(res["combined"]))
# doubly nested result names
infix = Word(nums)("infix")
pfx = Combine(Literal("pfx") + infix)("prefix")
combined = Combine(pfx + Word(alphas))("combined")
combined.runTests(text)
res = combined.parseString(text)
print("type(res[0]):", type(res[0]))
print('type(res["combined"]):', type(res["combined"]))
print('type(res["combined"]["prefix"]):', type(res["combined"]["prefix"]))
# no nested result names
pfx = Literal("pfx") + Word(nums)
combined = Combine(pfx + Word(alphas))("combined")
combined.runTests(text)
res = combined.parseString(text)
print("type(res[0]):", type(res[0]))
print('type(res["combined"]):', type(res["combined"]))
生产
pfx01ABC
[['pfx01ABC']]
- combined: ['pfx01ABC']
- prefix: 'pfx'
type(res[0]): <class 'pyparsing.ParseResults'>
type(res["combined"]): <class 'pyparsing.ParseResults'>
pfx01ABC
[['pfx01ABC']]
- combined: ['pfx01ABC']
- prefix: ['pfx01']
- infix: '01'
type(res[0]): <class 'pyparsing.ParseResults'>
type(res["combined"]): <class 'pyparsing.ParseResults'>
type(res["combined"]["prefix"]): <class 'pyparsing.ParseResults'>
pfx01ABC
['pfx01ABC']
- combined: 'pfx01ABC'
type(res[0]): <class 'str'>
type(res["combined"]): <class 'str'>
是因为结果名称与嵌套解析器一起存储而不是全局存储吗?
在双重嵌套结果名称的情况下,结果具有与在单个嵌套结果名称的情况下相同数量的封闭方括号。假设上述情况,我的期望是带有一组附加方括号的结果。为什么不是这样?
解决方案
推荐阅读
- java - Java Jersey Cient 中的字段套管问题
- css - 样式嵌套的 Web 组件,其中子组件可以与父组件具有相同的类型,但需要与父组件不同的样式
- swift - swift 和 PDFkit 创建特定格式
- pandas - 从 Panda DataFrame 中的组中删除异常值
- python - python beautifulsoup 从 javascript 获取密钥到 JSON 对象
- firebase - 使用 Firestore 触发器处理 Cloud Firestore 中单个集合的所有现有数据
- google-sheets - 使用 vlookup 从谷歌表格上的不同工作簿进行更新
- c# - EFCore 在 automapper 上的绑定器设置 int?=> 整数?将 null 值设为 0 而不是 null
- typescript - 如何获取 Powershell 脚本输出 fin Typescript 文件?
- javascript - 在 webpack 配置中反应的 jsx 是错误的,这表明没有配置加载程序来处理这个文件