Use Python to search one .txt file for a list of words or phrases (and show the context)
Posted
by prupert
on Stack Overflow
See other posts from Stack Overflow
or by prupert
Published on 2010-06-09T16:39:11Z
Indexed on
2010/06/09
16:42 UTC
Read the original article
Hit count: 115
Basically as the question states. I am fairly new to Python and like to learn by seeing and doing.
I would like to create a script that searches through a text document (say the text copied and pasted from a news article for example) for certain words or phrases. Ideally, the list of words and phrases would be stored in a separate file.
When getting the results, it would be great to get the context of the results. So maybe it could print out the 50 characters in the text file before and after each search term that has been found. It'd be cool if it also showed what line the search term was found on.
Any pointers on how to code this, or even code examples would be much appreciated.
© Stack Overflow or respective owner