Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- def search(lines, pattern, history=5):
- previous_lines = deque(maxlen=history)
- for line in lines:
- if pattern in line:
- yield line, previous_lines
- previous_lines.append(line)
- if __name__ == 'main':
- with open('some_text_file.txt') as f:
- for line, previous_line in search(f, 'python', 5):
- for p_line in previous_line:
- print(p_line, end='')
- print(line, end='')
- print('-' * 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement