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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:41:12+00:00 2026-05-11T16:41:12+00:00

I need to identify some locations in a file where certain markers might be.

  • 0

I need to identify some locations in a file where certain markers might be. I started off thinking that I would use list.index but I soon discovered that returns the first (and only the first) item. so I decided to implement my own solution which was

count=0
docIndex=[]
for line in open('myfile.txt','r'):
    if 'mystring' in line:
        docIndex.append(count)
    count+=1

But this is Python right. There has to be a simpler solution since well it is Python. Hunting around this site and the web I came up with something slightly better

newDocIndex=[]
for line in fileinput.input('myfile',inplace=1):
    if 'mystring' in line:
    newDocIndex.append(fileinput.lineno())

I know this is too much info but since I finished grading finals last night I thought well-this is Python and we want to make some headway this summer-lets try a list comprehension

so I did this:

[fileinput.lineno() for line in fileinput.input('myfile',inplace=1) if 'mystring' in line]

and got an empty list. So I first guessed that the problem is that the item in the for has to be the item that is used to build the list. That is if I had line instead of fileinput.lineno() I would have had a non-empty list but that is not the issue.

Can the above process be reduced to a list comprehension?

Using the answer but adjusting it for readability

listOfLines=[lineNumb for lineNumb,dataLine in enumerate(open('myfile')) if 'mystring' in dataLine]
  • 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-11T16:41:12+00:00Added an answer on May 11, 2026 at 4:41 pm

    What about this?

    [index for index,line in enumerate(open('myfile.txt')) if 'mystring' in line]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to uniquely identify and store some URLs. The problem is that sometimes
We need to identify and then process switch/case statements in code based on some
After eliminating some specific nonsense pattern, I need to identify and possibly delete fields
I need to identify the P(rocess) ID of an executing batch file from a
I am trying to copy file(s) using webrequest inside WCF. I need some suggestions.
We're trying to identify the locations of certain information stored across our enterprise in
I'm dealing with a legacy system where I need to identify some bad records
I'd like to identify some types using a service name. I need exactly what
I need to identify my pages with something that can be picked up by:
I need to identify some wnds in my application (objects of CMDIChildWnd class). To

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.