简体   繁体   中英

Pyparsing two-dimensional list

I have the following sample data:

165 150 238 402 395 571 365 446 284 278 322 282 236 
16 5 19 10 12 5 18 22 6 4 5 
259 224 249 193 170 151 95 86 101 58 49 
6013 7413 8976 10392 12678 9618 9054 8842 9387 11088 11393;

It is the equivalent of a two dimensional array (except each row does not have an equal amount of columns). At the end of each line is a space and then a \\n except for the final entry which is followed by no space and only a ; .

Would anyone know the pyparsing grammer to parse this? I've been trying something along the following lines but it will not match.

data = Group(OneOrMore(Group(OneOrMore(Word(nums) + SPACE)) + LINE) + \
           Group(OneOrMore(Word(nums) + SPACE)) + Word(nums) + Literal(";")

The desired output would ideally be as follows

[['165', '150', '238', '402', '395', '571', '365', '446', '284', '278', 
'322', '282', '236'], ['16', '5', ... ], [...], ['6013', ..., '11393']]

Any assistance would be greatly appreciated.

You can use the stopOn argument to OneOrMore to make it stop matching. Then, since newlines are by default skippable whitespace, the next group can start matching, and it will just skip over the newline and start at the next integer.

import pyparsing as pp

data_line = pp.Group(pp.OneOrMore(pp.pyparsing_common.integer(), stopOn=pp.LineEnd()))
data_lines = pp.OneOrMore(data_line) + pp.Suppress(';')

Applying this to your sample data:

data = """\
165 150 238 402 395 571 365 446 284 278 322 282 236 
16 5 19 10 12 5 18 22 6 4 5 
259 224 249 193 170 151 95 86 101 58 49 
6013 7413 8976 10392 12678 9618 9054 8842 9387 11088 11393;"""

parsed = data_lines.parseString(data)

from pprint import pprint
pprint(parsed.asList())

Prints:

[[165, 150, 238, 402, 395, 571, 365, 446, 284, 278, 322, 282, 236],
 [16, 5, 19, 10, 12, 5, 18, 22, 6, 4, 5],
 [259, 224, 249, 193, 170, 151, 95, 86, 101, 58, 49],
 [6013, 7413, 8976, 10392, 12678, 9618, 9054, 8842, 9387, 11088, 11393]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM