从parseAction返回未更改的标记会生成双重嵌套的字典

时间:2018-08-28 08:43:00

标签: python parsing pyparsing

将仅返回令牌的解析动作附加到解析器,该解析器在ParseResults列表中生成单个组,从而生成一个双重嵌套的字典(result3)。

我将此问题发布在pyparsing GitHub issues page上,将其视为不一致的地方。但是也许不是,我只是不理解ParseResults对象。在下面,我稍微扩展了我的观察范围。

from __future__ import print_function

from pyparsing import *

name = Word(alphas)('name')
score = Word(nums + '.')('score')
nameScore = Group(name + score)
line1 = nameScore('Rider')
line2 = nameScore('Rider') + nameScore('Bull')

result1 = line1.parseString('Mauney 46.5')
result2 = line2.parseString('Mauney 46.5 Asteroid 46')

print("### before parse action is added ###")
print("result1.dump():\n" + result1.dump() + "\n")
print("result2.dump():\n" + result2.dump() + "\n")

line1.setParseAction(lambda t: t)
line2.setParseAction(lambda t: t)

result3 = line1.parseString('Mauney 46.5')
result4 = line2.parseString('Mauney 46.5 Asteroid 46')

print("### after parse action was added ###")
print("result3.dump():\n" + result3.dump() + "\n")
print("result4.dump():\n" + result4.dump() + "\n")

执行上面的结果如下。

### before parse action is added ###
result1.dump():
[['Mauney', '46.5']]
- Rider: ['Mauney', '46.5']
  - name: 'Mauney'
  - score: '46.5'

result2.dump():
[['Mauney', '46.5'], ['Asteroid', '46']]
- Bull: ['Asteroid', '46']
  - name: 'Asteroid'
  - score: '46'
- Rider: ['Mauney', '46.5']
  - name: 'Mauney'
  - score: '46.5'

### after parse action was added ###
result3.dump():
[['Mauney', '46.5']]
- Rider: [['Mauney', '46.5']]   <=================
  - Rider: ['Mauney', '46.5']
    - name: 'Mauney'
    - score: '46.5'

result4.dump():
[['Mauney', '46.5'], ['Asteroid', '46']]
- Bull: ['Asteroid', '46']
  - name: 'Asteroid'
  - score: '46'
- Rider: ['Mauney', '46.5']
  - name: 'Mauney'
  - score: '46.5'

最外面的ParseResults对象似乎是其自己的字典的一部分。

这是预期的行为吗?如果是,为什么会发生?

一些进一步的观察结果。

如果“ ParseResults”列表包含两组,则不会出现双重嵌套(结果2)。

如果对line1的解析操作更改为

line1.setParseAction(lambda t: [t[0]])

获得所需的结果:

result1.dump():
[['Mauney', '46.5']]
- Rider: ['Mauney', '46.5']
  - name: 'Mauney'
  - score: '46.5'

0 个答案:

没有答案