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.