简体   繁体   English

解析二维列表

[英]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 ; 每行的末尾是一个空格,然后是一个\\n ,最后一个条目除外,后面没有空格,只有一个; .

Would anyone know the pyparsing grammer to parse this? 有人知道pyparsing语法分析器来解析吗? 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. 您可以使用stopOn参数来OneOrMore ,以使其停止匹配。 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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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