Skip to Content
Getting Started with Pyparsing
book

Getting Started with Pyparsing

by Paul McGuire
October 2007
Intermediate to advanced
65 pages
1h 33m
English
O'Reilly Media, Inc.
Content preview from Getting Started with Pyparsing

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: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Undocumented Secrets of MATLAB-Java Programming

Undocumented Secrets of MATLAB-Java Programming

Yair M. Altman
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz

Publisher Resources

ISBN: 9780596514235Errata