Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task03 completed #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions task-03/get_top_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
https://github.com/astropgh/learning-by-doing/tree/master/task-03
"""

def extract_data_lines(filename, start_text, end_text):
def extract_data_lines(filename, start_text, end_text, include_start, include_end):
"""
open `filename`, and yield the lines between
the line that contains `start_text` and the line that contains `end_text`
"""
# fill in code as needed
with open(filename) as fh:
on = False
for line in fh:
# fill in code as needed
# use `yield line` to return desired lines but keep the function going
if start_text in line:
on = True
if not include_start:
continue
if end_text in line and not include_end:
break
if on:
yield line
if end_text in line and include_end:
break


if __name__ == '__main__':
filename = 'top5names.html'
start_text = '<tr><td align="center">2017</td>'
end_text = '</table></center></div><!-- end #content -->'

for line in extract_data_lines(filename, start_text, end_text):
for line in extract_data_lines(filename, start_text, end_text, include_start=True, include_end=True):
print(line)