PyParsing:使用 SkipTo()、标记数据和可能的 Forward()
PyParsing: using SkipTo(), labeled data and possibly a Forward()
我正在尝试解析给定以下格式的输入文件。
file = "Begin 'big section header'
#... section contents ...
sub 1: value
sub 2: value
....
Begin 'interior section header'
....
End 'interior section header'
End 'big section header'"
到 return 贪婪地抓住标记部分 header 值
之间的所有内容的列表
['section header', ['section contents']]
我目前的尝试是这样的
import pyparsing as pp
begin = pp.Keyword('Begin')
header = pp.Word(pp.alphanums+'_')
end = pp.Keyword('End')
content = begin.suppress() + header + pp.SkipTo(end + header)
content.searchString(file).asList()
returns
['section header', ['section contents terminated at the first end and generic header found']]
我怀疑我的语法需要更改为某种形式
begin = pp.Keyword('Begin')
header = pp.Word(pp.alphanums+'_')
placeholder = pp.Forward()
end = pp.Keyword('End')
placeholder << begin.suppress() + header
content = placeholder + pp.SkipTo(end + header)
但我终其一生都无法找出对 Forward object 的正确分配,这并没有给我已有的东西。
在这种情况下比 Forward
更容易使用 matchPreviousLiteral
:
content = begin.suppress() + header + pp.SkipTo(end + matchPreviousLiteral(header))
你匹配的是任意end
,但是你想要的是匹配前面begin
.[=16=的end
]
我正在尝试解析给定以下格式的输入文件。
file = "Begin 'big section header'
#... section contents ...
sub 1: value
sub 2: value
....
Begin 'interior section header'
....
End 'interior section header'
End 'big section header'"
到 return 贪婪地抓住标记部分 header 值
之间的所有内容的列表['section header', ['section contents']]
我目前的尝试是这样的
import pyparsing as pp
begin = pp.Keyword('Begin')
header = pp.Word(pp.alphanums+'_')
end = pp.Keyword('End')
content = begin.suppress() + header + pp.SkipTo(end + header)
content.searchString(file).asList()
returns
['section header', ['section contents terminated at the first end and generic header found']]
我怀疑我的语法需要更改为某种形式
begin = pp.Keyword('Begin')
header = pp.Word(pp.alphanums+'_')
placeholder = pp.Forward()
end = pp.Keyword('End')
placeholder << begin.suppress() + header
content = placeholder + pp.SkipTo(end + header)
但我终其一生都无法找出对 Forward object 的正确分配,这并没有给我已有的东西。
在这种情况下比 Forward
更容易使用 matchPreviousLiteral
:
content = begin.suppress() + header + pp.SkipTo(end + matchPreviousLiteral(header))
你匹配的是任意end
,但是你想要的是匹配前面begin
.[=16=的end
]