我正在尝试了解如何在 pyparsing 中组织解析器。预期结果是没有重复且没有不必要嵌套的字典。为此,我编写了四个测试。他们的结果(在底部)似乎让我感到困惑。
四个不同的测试解析器( person1 - person4 )呈现出略有不同的结果。
我的问题:
person2
解析器在结果字典中的 person 对象之外还会产生另外的 name 和 surname 对?setResultsName()
会产生ParseResults
而不是字符串,作为person4
解析器结果列表中的第一个元素?ParseResults
推断解析器的组织方式?如果是这样,那怎么办?-
from __future__ import print_function
import json
from pyparsing import *
def test(person, result):
print(person)
print("result: {}".format(result))
print("result.asDict():\n" +
json.dumps(result.asDict(), sort_keys=True, indent=4))
print("type(result): " + str(type(result)))
print("type(result[0]): " + str(type(result[0])))
print("type(result['person']" + str(type(result["person"]))
if 'person' in result else '')
print()
name = Word(alphas).setResultsName("name")
surname = Word(alphas).setResultsName("surname")
person1 = Group(name + surname).setResultsName("person")
person2 = (name + surname).setResultsName("person")
person3 = Combine(name + White() + surname)
person4 = Combine(name + White() + surname).setResultsName("person")
people = [person1, person2, person3, person4]
parsers = ['Group(name + surname).setResultsName("person")',
'(name + surname).setResultsName("person")',
'Combine(name + White() + surname)',
'Combine(name + White() + surname).setResultsName("person")',
]
enum = 1
for p in people:
test("person" + str(enum) + ": " +
parsers[enum - 1], p.parseString("John Doe"))
enum += 1
执行上述结果将得到以下输出:
person1: Group(name + surname).setResultsName("person")
result: [['John', 'Doe']]
result.asDict():
{
"person": {
"name": "John",
"surname": "Doe"
}
}
type(result): <class 'pyparsing.ParseResults'>
type(result[0]): <class 'pyparsing.ParseResults'>
type(result['person']<class 'pyparsing.ParseResults'>
person2: (name + surname).setResultsName("person")
result: ['John', 'Doe']
result.asDict():
{
"name": "John",
"person": {
"name": "John",
"surname": "Doe"
},
"surname": "Doe"
}
type(result): <class 'pyparsing.ParseResults'>
type(result[0]): <type 'str'>
type(result['person']<class 'pyparsing.ParseResults'>
person3: Combine(name + White() + surname)
result: ['John Doe']
result.asDict():
{
"name": "John",
"surname": "Doe"
}
type(result): <class 'pyparsing.ParseResults'>
type(result[0]): <type 'str'>
person4: Combine(name + White() + surname).setResultsName("person")
result: [['John Doe']]
result.asDict():
{
"person": {
"name": "John",
"surname": "Doe"
}
}
type(result): <class 'pyparsing.ParseResults'>
type(result[0]): <class 'pyparsing.ParseResults'>
type(result['person']<class 'pyparsing.ParseResults'>