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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:11:16+00:00 2026-05-23T23:11:16+00:00

I have a 30000 plus line text file that I need to modify using

  • 0

I have a 30000 plus line text file that I need to modify using Python. What I want to do is the following:

  1. There is a specific keyword1 in the file that has a certain device name following the keyword1. I need to identify keyword1 in all lines and retrieve all device names, then store them in a list for later use

    I have achieved this by using Regular Expression

  2. Once I have the device names list, I need to insert a newline with “fixed format + device name” into the file at a series of specific locations following keyword2

    This is where I run into problem.

    Originally I used the simple counting method. I pattern.search(line) throughout the file and once the keyword1 is identified, I locate the inserting location by counting i + 5, where i is the index of the line where keyword1 is identified. However, it turns out that the line order in the file does matter so I will need to insert the newline only after the line where keyword2 is located. What’s more troublesome is that keyword2 exists everywhere in the file. It is only those keyword2 within 7 lines after keyword1 that needs to be considered.

To give an example:

 This is a random line with KEYWORD2      <--- this keyword2 is not considered
 This is a random line
 This is a random line
 This is a random line, KEYBOARD1 "DEVICE NAME"     <--- id keyword1 and record DEVICE
 This is a random line
 This is a random line
 This is a random line
 This is a random line
 This is a random line with KEYWORD2      <--- this keyword2 is considered

Any suggestion is appreciated. Thanks in advance

  • 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-23T23:11:16+00:00Added an answer on May 23, 2026 at 11:11 pm

    I … think you should probably approach it with something like this:

    with open('input.txt') as fhi, open('output.txt', 'w') as fho:
      for line in fhi:
        if not pattern.search(line):
          # if there is no match write the line to the output file and proceed.
          fho.write(line)
          continue
    
        # if we get this far we found a match.  Scan up to seven lines.
        for i in range(7):
          toCheck = next(fhi)
    
          if not pattern2.search(toCheck):
            # if we don't find the 2nd keyword write the line, continue the sub-loop.
            fho.write(toCheck)
            continue
    
          # if we get this far we found the second pattern.  Add our newline.
          fho.write(toCheck)
          fho.write('\r\n')
    

    This is using the multiple-file syntax for with that was introduced in 2.7. In earlier versions you’ll have to nest the with statements or manage your file handles manually.

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

Sidebar

Related Questions

I have a file of about 30000 lines of data that I want to
I have an array with 30000 plus entries that need to go into a
I have the following code that I want to be able to loop every
I have 30,000 rows in a database that need to be similarity checked (using
I have a DataTable with column Value and column Name that has 30000 rows.
I have a huge file (~26 MB) with around 200 columns & 30000 records.
I'm making dictionary, it have over 50000 rows. Now, I'm using sqlite for 30000
I have created a query block with begin/end and want to run that in
I have a GridView which displays in excess of 30000 rows, and users need
I have a regex that I thought was working correctly until now. I need

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.