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

  • Home
  • SEARCH
  • 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 8368081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:18:17+00:00 2026-06-09T13:18:17+00:00

Using Python, I need to search a text file for a string that/ I

  • 0

Using Python, I need to search a text file for a string “that/”

I then need get the line number (currently doing so by using enumerate), and print off
“that\” + the remaining text until the next space.

Example text: First/LS thing/NN I/PRP want/VBP to/TO ask/VB is/VBZ if/IN you/PRP remember/VBP any/DT books/NNS that/IN you/PRP read/VBP as/IN a/DT child/NN

Example output: that/IN 14

Here is the code I have right now, which functions correctly, but wherever it should print “that/xx” it prints nothing.

with open(filename) as f:
            for num, line in enumerate(f, 1):
                if 'that/' in line:
                    myString = line
                    mySub = myString[myString.find('that/'):myString.find(' ')]
                    print(mySub, str(num))
                    formattedLines.append(mySub + ' ' + str(num) + '\n')
  • 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-09T13:18:18+00:00Added an answer on June 9, 2026 at 1:18 pm

    I think the problem is that the whitespace can happen before the that/. Start your search for the whitespace at the index where that/ was found:

    with open(filename) as f:
        for num, line in enumerate(f, 1):
            if 'that/' in line:
                myString = line
                where_is_that = myString.find('that/')
                mySub = myString[where_is_that:myString.find(' ', where_is_that)]
                print(mySub, str(num))
                formattedLines.append(mySub + ' ' + str(num) + '\n')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 30000 plus line text file that I need to modify using
I need to get the line number of a phrase in a text file.
I have a process that pickles a dictionary using Python 3.2. I then need
I need to search for certain lines in a file that contain numbers, then
Using Python I need to insert a newline character into a string every 64
Using Python I need to delete all characters in a multiline string up to
i need to check existing of Character (*,&,$) in Given String using python command
I'm using Python to parse a file in search for e-mail addresses, but I
I need to split a string using python but only on the first instance
Ok so I need to download some web pages using Python and did a

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.