这是一个DSL的片段,我正在尝试使用pyparing来解析它。
我有一个<keyword> 02 01 30 03 40 20 10格式的字符串
哪里
02是字符串的数目
01是string1的长度(以字节为单位)
30本身就是string1
03是string2的长度(以字节为单位)
40 20 10是string2
如何使用string解析来标记此字符串?
发布于 2013-09-15 07:35:16
所以它是一个计数阵列的countedArray?你试过:
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()给予:
['key', [['30'], ['40', '20', '10']]]命名的结果还允许您按名称获取分析过的位:
result = lineExpr.parseString(test)
print result.keyword
print result.data给予:
key
[[['30'], ['40', '20', '10']]]https://stackoverflow.com/questions/18808363
复制相似问题