I am looking for some words in a file in python. After I find each word I need to read the next two words from the file. I’ve looked for some solution but I could not find reading just the next words.
# offsetFile - file pointer
# searchTerms - list of words
for line in offsetFile:
for word in searchTerms:
if word in line:
# here get the next two terms after the word
Thank you for your time.
Update: Only the first appearance is necessary. Actually only one appearance of the word is possible in this case.
file:
accept 42 2820 access 183 3145 accid 1 4589 algebra 153 16272 algem 4 17439 algol 202 6530
word: [‘access’, ‘algebra’]
Searching the file when I encounter ‘access’ and ‘algebra’, I need the values of 183 3145 and 153 16272 respectively.
An easy way to deal with this is to read the file using a generator that yields one word at a time from the file.
Then to find the word you’re interested in and read the next two words:
Now
foundwords[0]is the word you found,foundwords[1]is the word after that, andfoundwords[2]is the second word after it. If there aren’t enough words, then one or more elements of the list will beNone.It is a little more complex if you want to force this to match only within one line, but usually you can get away with considering the file as just a sequence of words.