Python Pyparsing:setResultsName存在问题

Python Pyparsing:setResultsName存在问题,python,pyparsing,Python,Pyparsing,我正在解析带有多个答案的多项选择题,答案如下: ParserElement.setDefaultWhitespaceChars(u""" \t""") in_ = """1) first stem. = option one one key = option one two key - option one three distractor = option one four key 2) second stem ? - option two one distractor - option tw

我正在解析带有多个答案的多项选择题,答案如下:

ParserElement.setDefaultWhitespaceChars(u""" \t""")
in_ = """1) first stem.
= option one one key
= option one two key
- option one three distractor
= option one four key
2) second stem ?
- option two one distractor
- option two two distractor
= option one three key
3) third stem.
- option three one key
= option three two distractor
"""
newline = Suppress(u"\n")
end_number = Suppress(oneOf(u') / ('))
end_stem = Suppress(oneOf(u"? .")) + newline
end_phrase = Optional(u'.').suppress() + newline
phrase = OneOrMore(Word(alphas)) + end_phrase
prefix = Word(u"-", max=1)('distractor') ^ Word(u"=", max=1)('key')
stem = Group(OneOrMore(Word(alphas))) + end_stem
number = Word(nums) + end_number
question = number + stem('stem') +
    Group(OneOrMore(Group(prefix('prefix') + phrase('phrase'))))('options')
等号代表正确答案,破折号代表干扰

我的语法是这样的:

ParserElement.setDefaultWhitespaceChars(u""" \t""")
in_ = """1) first stem.
= option one one key
= option one two key
- option one three distractor
= option one four key
2) second stem ?
- option two one distractor
- option two two distractor
= option one three key
3) third stem.
- option three one key
= option three two distractor
"""
newline = Suppress(u"\n")
end_number = Suppress(oneOf(u') / ('))
end_stem = Suppress(oneOf(u"? .")) + newline
end_phrase = Optional(u'.').suppress() + newline
phrase = OneOrMore(Word(alphas)) + end_phrase
prefix = Word(u"-", max=1)('distractor') ^ Word(u"=", max=1)('key')
stem = Group(OneOrMore(Word(alphas))) + end_stem
number = Word(nums) + end_number
question = number + stem('stem') +
    Group(OneOrMore(Group(prefix('prefix') + phrase('phrase'))))('options')
当我分析结果时:

for match, start, end in question.scanString(in_):
    for o in match.options:
        try:
            print('key', o.prefix.key)
        except:
            print('distractor', o.prefix.distractor)
我得到:

AttributeError: 'unicode' object has no attribute 'distractor'

我很确定结果名是可链接的。如果是,我做错了什么?我可以很容易地解决这个问题,但如果不知道我做错了什么以及我误解了什么,那就不令人满意了。

问题是,
o
实际上是前缀——当你调用
o.prefix
时,你实际上比你需要的更深一层,并且正在检索前缀映射到的字符串,不是ParseResults对象

您可以通过修改代码来看到这一点,以便它打印出解析树:

for match, start, end in question.scanString(in_):
    for o in match.options:
        print o.asXML()
        try:
            print('key', o.prefix.key)
        except:
            print('distractor', o.prefix.distractor)
然后,代码将打印出来:

<prefix>
  <key>=</key>
  <phrase>option</phrase>
  <ITEM>one</ITEM>
  <ITEM>one</ITEM>
  <ITEM>key</ITEM>
</prefix>
Traceback (most recent call last):
  File "so07.py", line 37, in <module>
    print('distractor', o.prefix.distractor)
AttributeError: 'str' object has no attribute 'distractor'

回答不错-我建议使用
dump()
而不是
asXML()
,因为后者有点问题。