Parsing Data from a Table—Using Parse Actions and ParseResults
As our first example, let's look at a simple set of scores for college football games that might be given in a datafile. Each row of text gives the date of each game, followed by the college names and each school's score.
09/04/2004 Virginia 44 Temple 14 09/04/2004 LSU 22 Oregon State 21 09/09/2004 Troy State 24 Missouri 14 01/02/2003 Florida State 103 University of Miami 2
Our BNF for this data is simple and clean:
digit ::= '0'..'9' alpha ::= 'A'..'Z' 'a'..'z' date ::= digit+ '/' digit+ '/' digit+ schoolName ::= ( alpha+ )+ score ::= digit+ schoolAndScore ::= schoolName score gameResult ::= date schoolAndScore schoolAndScore
We begin building up our parser by converting these BNF definitions into pyparsing class instances. Just as we did in the extended "Hello, World!" program, we'll start by defining the basic building blocks that will later get combined to form the complete grammar:
# nums and alphas are already defined by pyparsing num = Word(nums) date = num + "/" + num + "/" + num schoolName = OneOrMore( Word(alphas) )
Notice that you can compose pyparsing expressions using the +
operator to combine pyparsing expressions and string literals. Using these basic elements, we can finish the grammar by combining them into larger expressions:
score = Word(nums) schoolAndScore = schoolName + score gameResult = date + schoolAndScore + schoolAndScore
We use the gameResult
expression to parse the individual lines of the input text: ...
Get Getting Started with Pyparsing now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.