我使用pyparsing
来解析大量文本并获取一些数字。
我正在解析的文本是这样的:
asdkjh 1 120 203
kajshd 230 2309 2309
Activated Attempts 230 2309 2309
Activated Attempts 230 2309 2309
aksjdh 300
...
我需要搜索一个字符串并捕获紧跟在给定字符串后面的所有值。我写的代码看起来像这样,工作正常。
returnValue= 0
integer = pyparsing.Word(pyparsing.nums).setParseAction(lambda toks: int(toks[0]))
attempted = integer.setResultsName("attempted")
text = "Activated Attempts"
row = text + attempted
table = pyparsing.ZeroOrMore(pyparsing.Group(row) | pyparsing.SkipTo(row).suppress())
attempteds = [row.asDict() for row in table.parseString(self.sendLine("lts_pm p"))]
for attempted in attempteds:
returnValue+= attempted["attempted"]
return returnValue
在上面的情况下,它将返回460。 上面的函数,搜索给定的“Activated Attempts”并存储该文本后面的数字,汇总数字和返回。
但是我需要在同一个脚本中添加更多搜索查询,我尝试了:
text = pyparsing.Keyword("Activated Attempts") or pyparsing.Keyword("Non Attempts")
但脚本只捕获“Activated Attempts”并返回其编号并完全忽略第二个文本。
如果没有Keyword
,有什么用?我也试过Literal
,但也没有成功!
答案 0 :(得分:4)
from pyparsing import *
data = '''
asdkjh 1 120 203
kajshd 230 2309 2309
Activated Attempts 230 2309 2309
Activated Attempts 230 2309 2309
aksjdh 300
'''
eventParser = Group(Word(alphas) + Optional(Word(alphas)))
rowParser = Group(eventParser + delimitedList(Word(nums),White(" ")))
tableParser = ZeroOrMore(rowParser)
def getValue(attemptsList, term):
value = 0
for attempt in attemptsList:
if ' '.join(attempt[0]) == term:
value += int(attempt[1])
return value
attempts = getValue(tableParser.parseString(data), "Activated Attempts")
print attempts
修改强>
来自文档
关键字 - 类似于Literal,但必须紧跟空格,标点符号或其他非关键字字符;防止意外匹配恰好以定义的关键字开头的非关键字。