【发布时间】:2019-05-12 05:21:24
【问题描述】:
我想在下一个条目开始后结束一个循环。例如,假设我有以下由三个文档组成的 txt 文件:
Document 1
text1
text1
tex1
Document 2
text2
text2
text2
Document 3
text3
text3
text3
我正在尝试构建一个JSON 文件,该文件将单个文章中的每个文本连接起来。例如'body' = text1 text1 text1; 'body' = text2 text2 text2;和'body' = text2 text2 text2。为此,我搜索单词Document,然后基本上将其后面的文本连接成一行。问题是我的代码跳过了一个文档,所以它只适用于文档 1 和 3:
for line in f:
if re.search(r"Document ", line):
text = ''
while not re.search(r"Document ", line):
text += line+' '
article['body'] = text
知道如何在下一个文档开始后告诉代码停止 (while not) 吗?
【问题讨论】:
标签: json regex python-3.x loops