Extract Paragraph with specific words between two similar titiles

白昼怎懂夜的黑 提交于 2019-12-02 06:11:48

To extract all summary sections that contain the words you are interested in:

split_on = 'summary\n\n'
must_contain = ['Project', 'Team Size']

with open('9.txt') as f_input, open('d.txt', 'w') as f_output:
    for part in f_input.read().split(split_on):
        if all(text in part for text in must_contain):
            f_output.write(split_on + part)

The second conditional statement here will never run, as it has an identical condition to the first one. Meaning copy will always be True after the first instance of summary.

if line.strip() == 'summary':
    re.compile('\r\nproject*\r\n')
    copy = True
elif line.strip() == "summary":
    copy =False 

What I'd recommend is having one statement that picks up the "summary" tags (I assume these are meant to be start/end of comment blocks) - and toggle copy.

To toggle a boolean, you can simple set it to the inverse of itself:

 a = True
 a = not a
 # a is now False

For example:

 if line.strip() == 'summary':
    copy = not copy
 elif copy:
    outfile.write(line)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!