-1

I am trying to edit code from a similar stack overflow question, found here: Extract Values between two strings in a text file using python

And I cannot figure out why this is not looping through my list of start and end to copy all my text. Here's my barely modified code.

intakedoc = 'Minnie.txt'
savedfile = "Mouse.txt"

start = ["Complaint", "Health History", "Psychiatric History and Treatment", "Education", "Drug and Alcohol Use", "Additional Personal Information", "Clinical Review of Systems", "Mental Status Examination" ]
end = ["Health History", "Psychiatric History and Treatment", "Education", "Drug and Alcohol Use", "Additional Personal Information", "Clinical Review of Systems", "Mental Status Examination", "Risk Assessments"]


with open(intakedoc, 'r') as infile, open(savedfile, 'w') as outfile:
    copy = False
    for line in infile:
        if line.strip() == start:
            copy = True
            outfile.write(line)
        elif line.strip() == end:
            copy = False
            outfile.write(line)
        elif copy:
            outfile.write(line)
outfile.close()

Example from text file:

Complaint / History Of Present Illness
30 year old female complains of ADHD,anxiety,depression for years.  The onset is described as childhood.  Progression is unchanging.  Severity is described as mild.  Patient described the following signs and symptoms: aDHD.

Health History
Past Medical History
Denies Negative Past Medical Hx

Psychiatric History and Treatment
Psychiatric History
ADHD
Pediatrician when a kid said ADHD. 
Anxiety
postpartum anxiety/ dep. Saw a counselor for that.
Depression and related disorders
postpartum depression.
quamrana
  • 37,849
  • 12
  • 53
  • 71

2 Answers2

0

Shouldn't you change the line

if line.strip() == start:

to

if line.strip() in start:
chatax
  • 990
  • 3
  • 17
0

The line: for line in infile: reads the file line by line assigning line to a string each time.

However, the line: if line.strip() == start: is essentially comparing a string (line.strip()) to a list, ie start. These will never compare True and so essentially your program never writes to the output file, even though it loops through every line of the input file.

quamrana
  • 37,849
  • 12
  • 53
  • 71