Python Forum

Full Version: How do I extract specific lines from HTML files before and after a word?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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(itertools.islice(f, 10))
        results = before.append(line)
I am currently getting a bunch of rows that say "None" in my terminal when I print the results. What is the issue here?
Why do you expect that "append" method returns a value?
Nothing about the value returned. In case if a function doesn't return a result python always returns None.