Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyparsing:nestedcountedarray?_Python_Pyparsing - Fatal编程技术网

Python pyparsing:nestedcountedarray?

Python pyparsing:nestedcountedarray?,python,pyparsing,Python,Pyparsing,这是我试图使用pyparsing解析的DSL的一个片段 我有一个字符串的格式是020130 03 40 20 10 在哪里 02是字符串数 01是string1的长度(以字节为单位) 30是字符串1本身 03是string2的长度(以字节为单位) 402010是字符串2 如何使用pyparsing标记这个字符串?所以它是countedArray的countedArray?您是否尝试过: from pyparsing import Word,nums,alphas,countedArray te

这是我试图使用pyparsing解析的DSL的一个片段

我有一个字符串的格式是
020130 03 40 20 10

在哪里
02
是字符串数
01
是string1的长度(以字节为单位)
30
是字符串1本身
03
是string2的长度(以字节为单位)
402010
是字符串2


如何使用pyparsing标记这个字符串?

所以它是countedArray的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']]]
key
[[['30'], ['40', '20', '10']]]
命名结果还允许您按名称获取解析的位:

result = lineExpr.parseString(test)
print result.keyword
print result.data
给出:

['key', [['30'], ['40', '20', '10']]]
key
[[['30'], ['40', '20', '10']]]

所以这是一个countedArray的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']]]
key
[[['30'], ['40', '20', '10']]]
命名结果还允许您按名称获取解析的位:

result = lineExpr.parseString(test)
print result.keyword
print result.data
给出:

['key', [['30'], ['40', '20', '10']]]
key
[[['30'], ['40', '20', '10']]]

这并不是你问题的答案,因为它不使用pyparsing,但为什么不使用.split()?@DHandle这就是我放弃后正在做的:)我认为更好地学习pyparsing它不是你问题的答案,因为它不使用pyparsing,但为什么不使用.split()呢“?@DHandle这就是我放弃后正在做的:)我想学一点更好的方法会更好。我从来没有想过这样使用它。对于嵌套数组,我正在考虑类似的方法。感谢您的回答,很多解析器开发都涉及到由内而外的思考,即如何将小部分构建成更大的部分。当我看到你的帖子时,我看到内部的字符串是整数数组,而那些被计数的数组的列表前面是字符串的数量,所以是一个外部的计数数组。在那之后,解析器基本上是自己编写的。所有的计算机科学都是这样的。是的,除了写作本身。我向你的表达能力鞠躬。我从来没有想过要这样使用它。对于嵌套数组,我正在考虑类似的方法。感谢您的回答,很多解析器开发都涉及到由内而外的思考,即如何将小部分构建成更大的部分。当我看到你的帖子时,我看到内部的字符串是整数数组,而那些被计数的数组的列表前面是字符串的数量,所以是一个外部的计数数组。在那之后,解析器基本上是自己编写的。所有的计算机科学都是这样的。是的,除了写作本身。我向你的表达能力致敬。