summaryrefslogtreecommitdiff
path: root/examples/parseTabularData.py
blob: 3c898e08bf944d403862759bbf14bff8597e741f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# parseTabularData.py
#
# Example of parsing data that is formatted in a tabular listing, with
# potential for missing values. Uses new addCondition method on
# ParserElements.
#
# Copyright 2015, Paul McGuire
#
from pyparsing import col, Word, Optional, alphas, nums

table = """\
         1         2
12345678901234567890
COLOR      S   M   L
RED       10   2   2
BLUE           5  10
GREEN      3       5
PURPLE     8"""

# function to create column-specific parse conditions
def mustMatchCols(startloc, endloc):
    return lambda s, l, t: startloc <= col(l, s) <= endloc


# helper to define values in a space-delimited table
# (change empty_cell_is_zero to True if a value of 0 is desired for empty cells)
def tableValue(expr, colstart, colend):
    empty_cell_is_zero = False
    if empty_cell_is_zero:
        return Optional(
            expr.copy().addCondition(
                mustMatchCols(colstart, colend), message="text not in expected columns"
            ),
            default=0,
        )
    else:
        return Optional(
            expr.copy().addCondition(
                mustMatchCols(colstart, colend), message="text not in expected columns"
            )
        )


# define the grammar for this simple table
colorname = Word(alphas)
integer = Word(nums).setParseAction(lambda t: int(t[0])).setName("integer")
row = (
    colorname("name")
    + tableValue(integer, 11, 12)("S")
    + tableValue(integer, 15, 16)("M")
    + tableValue(integer, 19, 20)("L")
)

# parse the sample text - skip over the header and counter lines
for line in table.splitlines()[3:]:
    print(line)
    print(row.parseString(line).dump())
    print("")