我有一个文本文件test.txt
,其中包含以下数据:
content content
more content
content conclusion
==========
content again
more of it
content conclusion
==========
content
content
contend done
==========
我想得到一个由==========
分隔的块列表。在
对于上面的例子,我希望是这样的:
^{pr2}$另外,如果有人能分享一个执行此操作的一般过程(如果有的话),我将不胜感激。在
输出:
['content content\nmore content\ncontent conclusion', 'content again\nmore of it\ncontent conclusion', 'content\ncontent\ncontend done']
可以使用正则表达式基于3个或更多
=
字符拆分文件。然后用反斜杠替换新行:如果您知道等号的确切长度,则可以使用字符串拆分方法:
^{2}$另外请注意,反斜杠用于转义字符,如果在字符串中使用反斜杠,它将转义下一个字符,这意味着如果您的特殊字符不会被解释为其原始含义。在
因此,最好用另一个分隔符分隔行:
使用
split
方法。在相关问题 更多 >
编程相关推荐