Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:10:29+00:00 2026-06-02T08:10:29+00:00

I am looking for some words in a file in python. After I find

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:10:30+00:00Added an answer on June 2, 2026 at 8:10 am

    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.

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

    Then to find the word you’re interested in and read the next 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 there aren’t enough words, then one or more elements of the list will be None.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python 2.4.3 I need to read through some files (can be as large as
Looking for some help! I need to split a string at the last occurrence
Looking for some help with optimising the query below. Seems to be two bottlenecks
I'm looking to automate some web interactions, namely periodic download of files from a
I have a text file that looks similar to; section header 1: some words
I've been looking for a way to update my Twitter status from a Python
I'm looking for a fast way for multiple processes (in multiprocessing.Pool()) to read from
I am looking for a property in a font file (either from WPF's System.Windows.Media.Fonts
I have a question simple like that: Let user enter some words from keyboard,one
I'm looking for a tool that works on Windows to reformat some C++ code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.