[英]pyparsing: Nested countedArray?
This is a snippet of a DSL that I am attempting to parse using pyparsing 这是我尝试使用pyparsing解析的DSL的代码段
I have a string of the format <keyword> 02 01 30 03 40 20 10
我有一个格式为<keyword> 02 01 30 03 40 20 10
的字符串
Where 哪里
02
is the number of strings 02
是字符串数
01
is the length of string1 (in bytes) 01
是字符串01
的长度(以字节为单位)
30
is the string1 itself 30
是string1本身
03
is the length of string2 (in bytes) 03
是字符串2的长度(以字节为单位)
40 20 10
is the string2 40 20 10
是字符串2
How do I tokenize this string using pyparsing? 如何使用pyparsing标记此字符串?
So it's a countedArray of countedArray's? 所以这是countedArray的countedArray吗? Did you try: 你试过了吗:
from pyparsing import Word,nums,alphas,countedArray
test = "key 02 01 30 03 40 20 10"
integer = Word(nums)
# each string is a countedArray of integers, and the data is a counted array
# of those, so...
lineExpr = Word(alphas)("keyword") + countedArray(countedArray(integer))("data")
# parse the test string, showing the keyworod, and list of lists for the data
print lineExpr.parseString(test).asList()
Gives: 得到:
['key', [['30'], ['40', '20', '10']]]
The named results also let you get at the parsed bits by name: 命名结果还使您可以按名称获得解析的位:
result = lineExpr.parseString(test)
print result.keyword
print result.data
Gives: 得到:
key
[[['30'], ['40', '20', '10']]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.