【发布时间】:2017-01-30 10:32:05
【问题描述】:
我想逐行处理一个字符串,但我想启用多行支持。这是示例文本:
First line
Second line
{{{
these three lines
I want to process
together
}}}
Last Line
我希望多行从{{{ 开始并在}}} 结束
我以前是这样逐行处理的:
lines = [l for l in text.splitlines()]
print lines
现在这段代码输出:
['First line', 'Second line', '{{{', 'these three lines', 'I want to process', 'together', '}}}', 'Last Line']
我想以某种方式使lines 包含以下内容:
['First line', 'Second line', 'these three lines I want to process together', 'Last Line']
或者,更高级的示例
First Line
Second line
Third{{{line
fourth line
fifth}}}line
sixth line
在这种情况下,我希望行包含
['First Line', 'Second line', 'Third', 'line fourth line fifth', 'line', 'sixth line']
【问题讨论】:
-
尝试遍历当前输出,检查'{{{',然后连接所有行,直到你到达'}}}'。