Read the following word in a file in python

I am looking for a few words in a file in python. After I find each word, I need to read the next two words from the file. I was looking for some solution, but I could not find the reading of the following 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 required. In fact, in this case, only one occurrence of this word is possible.

File:

 accept 42 2820 access 183 3145 accid 1 4589 algebra 153 16272 algem 4 17439 algol 202 6530 

word: ['access', 'algebra']

Finding a file, when I come across "access" and "algebra", I need the values ​​183 3145 and 153 16272 respectively.

+7
source share
4 answers

An easy way to deal with this is to read the file using a generator that gives one word from the file.

 def words(fileobj): for line in fileobj: for word in line.split(): yield word 

Then, to find the word you are interested in and read the following two words:

 with open("offsetfile.txt") as wordfile: wordgen = words(wordfile) for word in wordgen: if word in searchterms: # searchterms should be a set() to make this fast break else: word = None # makes sure word is None if the word wasn't found foundwords = [word, next(wordgen, None), next(wordgen, None)] 

Now, foundwords[0] is the word you found, foundwords[1] is the word after that, and foundwords[2] is the second word after it. If words are not enough, then one or more list items will be None .

This is a bit trickier if you want to make it fit within just one line, but usually you can get away with treating the file as just a sequence of words.

+15
source

If you need to get only the first two words, just do this:

  offsetFile.readline (). split () [: 2]
+2
source
 word = '3' #Your word delim = ',' #Your delim with open('test_file.txt') as f: for line in f: if word in line: s_line = line.strip().split(delim) two_words = (s_line[s_line.index(word) + 1],\ s_line[s_line.index(word) + 2]) break 
+1
source
  def searchTerm(offsetFile, searchTerms): # remove any found words from this list; if empty we can exit searchThese = searchTerms[:] for line in offsetFile: words_in_line = line.split() # Use this list comprehension if always two numbers continue a word. # Else use words_in_line. for word in [w for i, w in enumerate(words_in_line) if i % 3 == 0]: # No more words to search. if not searchThese: return # Search remaining words. if word in searchThese: searchThese.remove(word) i = words_in_line.index(word) print words_in_line[i:i+3] 

For "access", "algebra" I get this result:

['access', '183', '3145']
['algorithm', '153', '16272']

+1
source

All Articles