解析器嵌套和结果解释

时间:2018-08-15 12:56:02

标签: python python-2.7 parsing pyparsing

我正在尝试了解如何在 pyparsing 中组织解析器。预期结果是没有重复且没有不必要嵌套的字典。为此,我编写了四个测试。他们的结果(在底部)似乎让我感到困惑。

四个不同的测试解析器( person1 - person4 )呈现出略有不同的结果。

我的问题:

  • 为什么person2解析器在结果字典中的 person 对象之外还会产生另外的 name surname 对?
  • 为什么添加setResultsName()会产生ParseResults而不是字符串,作为person4解析器结果列表中的第一个元素?
  • 是否可以从ParseResults推断解析器的组织方式?如果是这样,那怎么办?
  • wikispaces pyparsing page不再存在,我想知道除the documentation included in the packagepyparsing quick reference之外是否还有其他可用资源。

-

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'>

0 个答案:

没有答案