Aug-05-2019, 05:45 PM
(This post was last modified: Aug-05-2019, 05:45 PM by glittergirl.)
I am trying to extract the 10 lines before and after the word "apple" from a directory (with subdirectories) full of HTML files. I want to print out the lines into a CSV file. Ideally, the CSV file will contain two variables: 1) the HTML filename and 2) the 10 lines before and after the word "apple".
I have done the following:
I have done the following:
import glob
import collections
import itertools
import sys
import csv
for filepath in glob.glob('**/*.html', recursive=True):
with open(filepath) as f:
before = collections.deque(maxlen=10)
for line in f:
if 'apple' in line:
sys.stdout.writelines(before)
sys.stdout.write(line)
sys.stdout.writelines(itertools.islice(f, 10))
break
results = before.append(line)
print(results)I am currently getting a bunch of rows that say "None" in my terminal when I print the results. What is the issue here?
