【发布时间】:2021-01-14 06:39:14
【问题描述】:
我需要从特定区块获取信息,为什么遇到关键字就不能停止分析?
source_string = '''
Code: 49662 (ACTIVE)
****************** Source: ******************
Key: Value
Description: test description 1
Key: Value
****************** Base information ******************
Key: Value
Description: test description 2
Key: Value
****************** Additional information ******************
Key: Value
Description: test description 3
Key: Value
'''
grammar = pp.Combine(pp.OneOrMore( \
pp.CaselessLiteral('Code') + pp.Optional(':') + pp.restOfLine() \
+ pp.SkipTo(pp.CaselessLiteral('Base information')).suppress() \
^ pp.CaselessLiteral('Description') + pp.restOfLine() \
, stopOn = pp.CaselessLiteral('Additional information')
))
pprint(grammar.searchString(source_string).asList())
结果:
[['代码:49662(活动)'], ['描述:测试描述 2'], ['描述:测试描述3']]
我使用SkipTo()排除的“测试描述1”的第一个值,如何从结果中排除“测试描述3”?
【问题讨论】:
标签: python python-3.x python-2.7 parsing pyparsing