在Python中解析TCL列表 [英] Parsing TCL lists in Python
问题描述
例如,我需要在双花括号上分割以空格分隔的TCL列表...例如...
OUTPUT = """{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}} {{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}"""
这应该解析为...
OUTPUT = ["""{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}}""",
"""{{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}"""]
我尝试过...
import re
splitter = re.compile('}}\s+{{')
splitter.split(OUTPUT)
但是,这会修剪中间的括号...
['{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}',
'172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}']
我不知道如何只分割}} {{
之间的空格.我知道我可以手动作弊并插入丢失的牙套,但我宁愿找到一种简单的方法来有效地解析出这个括号.
是否可以使用re.split
(或其他一些python解析框架)解析OUTPUT
来获取任意数量的包含{{content here}}
的以空格分隔的行?
您可以修改正则表达式以使用肯定的超前/后断言,而不会使用任何字符串:
re.compile('(?<=}})\s+(?={{)')
I need to split space-delimited TCL lists on double braces... for instance...
OUTPUT = """{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}} {{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}"""
This should parse into...
OUTPUT = ["""{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}}}""",
"""{{172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}"""]
I have tried...
import re
splitter = re.compile('}}\s+{{')
splitter.split(OUTPUT)
However, that trims the braces in the center...
['{{172.25.50.10:01:01-Ethernet 172.25.50.10:01:02-Ethernet {Traffic Item 1}',
'172.25.50.10:01:02-Ethernet 172.25.50.10:01:01-Ethernet {Traffic Item 1}}}']
I can't figure out how to only split on the spaces between }} {{
. I know I can cheat and insert missing braces manually, but I would rather find a simple way to parse this out efficiently.
Is there a way to parse OUTPUT
with re.split
(or some other python parsing framework) for an arbitrary number of space-delimited rows containing {{content here}}
?
You could modify your regex to use positive lookahead/behind assertions, which don't consume any of the string:
re.compile('(?<=}})\s+(?={{)')
这篇关于在Python中解析TCL列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!