Reputation: 5747
I have used pyparsing
to parse a big chunk of text and get some numbers.
The text I am parsing is something like this:
asdkjh 1 120 203
kajshd 230 2309 2309
Activated Attempts 230 2309 2309
Activated Attempts 230 2309 2309
aksjdh 300
...
I needed to search for a string and catch all the values which follows right after the given string. The code I have written looks like this and it working fine.
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
In the case above it would return 460. The above function, searches for the given "Activated Attempts" and stores the numbers that is followed by that text, summarizes the numbers and returns.
However I need to add more search queries into the same script, and I tried:
text = pyparsing.Keyword("Activated Attempts") or pyparsing.Keyword("Non Attempts")
But the script only catches "Activated Attempts" and returns its number and ignores the second text completely.
What is the use of Keyword
if not this? I have also tried Literal
but no success with that either!
Upvotes: 3
Views: 1562
Reputation: 13699
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
Edit
From the docs
Keyword - similar to Literal, but must be immediately followed by whitespace, punctuation, or other non-keyword characters; prevents accidental matching of a non-keyword that happens to begin with a defined keyword.
Upvotes: 3