Extract Paragraph with specific words between two similar titiles

前端 未结 2 1647
再見小時候
再見小時候 2021-01-23 10:10

my text file contains, paragraphs something like this.

summary

A result oriented and dedicated professional with three years’ experience in Software Development         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 10:45

    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)
    

提交回复
热议问题