This is my first time using re package in python.
In order to understand it better, I decided to copy a poem into my file and play around with re.search() using different regular expressions.
I got the poem from following website and copied it into my text file: http://www.poets.org/poetsorg/poem-day
I have also referred to this, this, this and this in order to help resolve my issue.
Following is my code:
searchFile = open ('/Users/admin/Documents/Python/NLP/Chapter1-TextSample.txt', 'r')
for line in searchFile:
if re.search('[pP]igeons', line):
print line
The pigeons ignore us gently as we
scream at one another in the parking
lot of an upscale grocer.
Pigeons scoot,and finches hop, and cicadas shout and shed
themselves into loose approximations of what
we might have in a different time called heaven.
for line in searchFile:
if re.search('[pP]igeons', line):
print line
for line in searchFile:
print line
As you can see, when I search for the first time, I get correct results. No issues there. However, once I do the same search again or even if I simply try to print the lines of the file, nothing shows up. However, when I check the 'searchFile' object, it still exists as seen below:
In[23]: searchFile
Out[23]: <open file '/Users/admin/Documents/Python/NLP/Chapter1-TextSample.txt', mode 'r' at 0x103a85d20>
Can someone please highlight why does this happen? Am I missing something?