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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:09:25+00:00 2026-06-09T18:09:25+00:00

Is there a way to scan an entire text document and say find everything

  • 0

Is there a way to scan an entire text document and say find everything that says “lol” and replace it with the id value of the first previous chapter tag? maybe something like this.

python

x=open('source.txt')
lines = x.readlines()
for line in lines:
  if line.startswith('<text'):
    line.replace('lol', first previous chapter id value)
x.write(lines)
x.close()

source text

<chapter id="1">
<text class="lol">
<text class="lol">
<chapter id="2">
<text class="lol">
<text class="lol">
<chapter id="3">
<text class="lol">
<text class="lol">
<chapter id="4">
<text class="lol">
<text class="lol">

result text

<chapter id="1">
<text class="1">
<text class="1">
<chapter id="2">
<text class="2">
<text class="2">
<chapter id="3">
<text class="3">
<text class="3">
<chapter id="4">
<text class="4">
<text class="4">
  • 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-09T18:09:27+00:00Added an answer on June 9, 2026 at 6:09 pm

    Try that. Basically all you needed to do extra was find that chapter id. Also I’m assuming you know to write to file, hence why I just printed each line.

    import re
    with open('source.txt') as x:
        for line in x:
            if line.startswith('<chapter'):
                    id = re.findall('"([^"]*)"', line) #Grabs string between matching quotations
            if line.startswith('<text'):
                    line = line.replace('lol',id[0])
            print line[:-1]
    

    Output:

    <chapter id="1">
    <text class="1">
    <text class="1">
    <chapter id="2">
    <text class="2">
    <text class="2">
    <chapter id="3">
    <text class="3">
    <text class="3">
    <chapter id="4">
    <text class="4">
    <text class="4">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a public dropbox folder. Is there any way to programmatically scan that
I would like to know if there is any way to scan a text
Is there a way to scan a SQL Server to find all the stored
Is there a way to force Zxing library to scan text only? I am
Is there a way to translate a character to its corresponding hardware scan code
Is there a way to notify MediaScanner service on Android platform not to scan
I am trying to find a way to build a full-text index stored in-memory
Is there a way I can configure Visual Studio to automatically scan my source
Is there a way to scan the classpath for certain patterns of resources? I
Is there a way to limit the header files that Boost.Build recursively scans for

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.