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 4535422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:24:11+00:00 2026-05-21T14:24:11+00:00

i wonder how to know , a position inside the .txt when I read.

  • 0

i wonder how to know , a position inside the .txt when I read.

this is my txt

cat dog monkey bird

this my printing

Word: cat Position: line 1 , word 1 (1,1)

any idea?

  • 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-05-21T14:24:12+00:00Added an answer on May 21, 2026 at 2:24 pm

    This would work for this given file:

    blah bloo cake
    donky cat sparrow
    nago cheese
    

    The code:

    lcount = 1
    with open("file", "r") as f:
        for line in f:
            if word in line:
                testline = line.split()
                ind = testline.index("sparrow")
                print "Word sparrow found at line %d, word %d" % (lcount, ind+1)
                break
            else:
                lcount += 1
    

    Would print:

    Word sparrow found at line 2, word 3
    

    You should be able to modify this quite easily to make a function or different output I hope.

    Although I’m still really not sure if this is what you’re after…

    Minor edit:
    As a function:

    def findword(objf, word):
        lcount = 1
        found = False
        with open(objf, "r") as f:
            for line in f:
                if word in line: # If word is in line
                    testline = line.split()
                    ind = testline.index(word) # This is the index, starting from 0
                    found = True
                    break
                else:
                    lcount += 1
            if found:
                print "Word %s found at line %d, word %d" % (word, lcount, ind+1)
            else:
                print "Not found"
    

    Use:

    >>> findword('file', "sparrow")
    Word sparrow found at line 2, word 3
    >>> findword('file', "donkey")
    Not found
    >>> 
    

    Shrug Not the best method I’ll give it that, but then again it works.

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

Sidebar

Related Questions

I know that this is somewhat subjective, but I wonder if there is a
I know about the same origin policy, however I wonder if this is possible:
I know this is a rather academic question, but I wonder how one would
I wonder if someone knows if there is a pre-made solution for this: I
I wonder if this would be doable ? To insert an array into one
I wonder if we know why the reset to initial map state button /
I wonder to know if someone can help to understand if I'm doing well
I wonder if someone can help me. I know JS quite well but am
I wonder how spring and google app engine work together I know that google
I know just about the basic usage of COUNT(*) and I wonder if I

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.