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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:43:48+00:00 2026-06-10T07:43:48+00:00

I have the following text file: This is my text file NUM,123 FRUIT DRINK

  • 0

I have the following text file:

This is my text file
NUM,123
FRUIT
DRINK
FOOD,BACON
CAR
NUM,456
FRUIT
DRINK
FOOD,BURGER
CAR
NUM,789
FRUIT
DRINK
FOOD,SAUSAGE
CAR
NUM,012
FRUIT
DRINK
FOOD,MEATBALL
CAR

And I have the following list called ‘wanted’:

['123', '789']

What I’m trying to do is if the numbers after NUM is not in the list called ‘wanted’, then that line along with 4 lines below it gets deleted. So the output file will looks like:

This is my text file
NUM,123
FRUIT
DRINK
FOOD,BACON
CAR
NUM,789
FRUIT
DRINK
FOOD,SAUSAGE
CAR

My code so far is:

infile = open("inputfile.txt",'r')
data = infile.readlines()

for beginning_line, ube_line in enumerate(data):
    UNIT = data[beginning_line].split(',')[1]
    if UNIT not in wanted:
        del data_list[beginning_line:beginning_line+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-10T07:43:50+00:00Added an answer on June 10, 2026 at 7:43 am

    You shouldn’t modify a list while you are looping over it.

    What you could try is to just advance the iterator on the file object when needed:

    wanted = set(['123', '789'])
    
    with open("inputfile.txt",'r') as infile, open("outfile.txt",'w') as outfile: 
        for line in infile:
            if line.startswith('NUM,'):
                UNIT = line.strip().split(',')[1] 
                if UNIT not in wanted:
                    for _ in xrange(4):
                        infile.next()
                    continue
    
            outfile.write(line)
    

    And use a set. It is faster for constantly checking the membership.

    This approach doesn’t make you read in the entire file at once to process it in a list form. It goes line by line, reading from the file, advancing, and writing to the new file. If you want, you can replace the outfile with a list that you are appending to.

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

Sidebar

Related Questions

I have the following in a text file: This is some text for cv_1
So I have the following code where I should read a Text File (This
I have following patterns in a text file. ###################### # ADD=123 New Comment ######################
I have a file, called WrongFileTypeDetection.R, containing the following text: # This file is
Let's say I have the following text file: This is the first line of
Would appreciate any help with this.:) I have the following text file: roomA Springer,
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I have the following text file Eif2ak1.aSep07 Eif2ak1.aSep07 LOC100042862.aSep07-unspliced NADH5_C.0.aSep07-unspliced LOC100042862.aSep07-unspliced NADH5_C.0.aSep07-unspliced What I
I have the following text file 40 timesteps took 58.320842 seconds greetings 0 80
I have the following text in xml file: <Config Builder=LP Wizard> <Libraries> <Library Name=XCAMSource/>

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.