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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:16:20+00:00 2026-05-16T14:16:20+00:00

I have a large file called fulldataset . I would like to write lines

  • 0

I have a large file called fulldataset. I would like to write lines from fulldataset to a new file called newdataset. I only want to write the lines from fulldataset though that contain the id numbers present in the listfile. Also all the id numbers start with XY. The id numbers occur in the middle of each line though.

Here is an example line from list file:

Robert, Brown, "XY-12344343", 1929232, 324934923, 

Here is the program I have so far. It runs fine, but doesn’t write anything into the new file.

datafile = open('C:\\listfile.txt', 'r')
completedataset = open('C:\\fulldataset.txt', 'r')
smallerdataset = open('C:\\newdataset.txt', 'w')

matchedLines = []

for line in datafile:
    if line.find("XY"):
        matchedLines.append( line )

counter = 1
for line in completedataset:
    print counter
    counter +=1

    for t in matchedLines:
        if t in line:
            fulldataset.write(line)
            del line
            break

datafile.close()
completedataset.close()
fulldataset.close()

EDIT:

Ok here is the new program:

datafile = open('C:\\tryexcel33.txt', 'r')
completedataset = open('C:\\fulldataset.txt', 'r')
smallerdataset = open('C:\\newdataset.txt', 'w')


counter = 1
for line in completedataset:
    print counter
    counter +=1

    if any( id in line for id in datafile ):
        smallerdataset.write( line )
        break

datafile.close()
completedataset.close()
fulldataset.close()

I still don’t have anything being written to the new file. I think a problem might be that in the full file the id numbers have a ” in front of them but this doesn’t exist in the listfile. Any thoughts?

  • 1 1 Answer
  • 4 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-16T14:16:21+00:00Added an answer on May 16, 2026 at 2:16 pm

    I don’t understand your code. Here’s the code to do what you’ve asked:

    ids = set( datafile.readlines( ) )
    for line in fulldataset:
        if any( id in line for id in ids ):
            smallerdataset.write( line )
    

    EDIT: I did the best I could with incomplete data. The fact that the IDs in the fulldataset are prefixed with XY is irrelevant, since we are searching through the whole string anyway ("foo" in "XY-foo" is still true). If no lines are being written, that’s because the lines of datafile are not exactly IDs. Please post a sample from datafile.

    You are also reusing the variable line, which will make your code go wrong in mysterious ways.

    You also have a break statement, which will cause at most one line to be written. Why?


    EDIT

    Many apologies, I just re-read the code — for some reason I had assumed that datafile was a list. It’s actually a file, so my previous code won’t work. Please see the fixed code.

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

Sidebar

Related Questions

I have a large file I want to download from a server I have
I have a large 100mb file which I would like to perform about 5000
I would like to have a function called in an onclick handler, within an
Hoping for some assistance here. I have a large xml file (called xml-file.xml in
I have a large XML data file (>160M) to process, and it seems like
I have a large file (5Gb) called my_file . I have a list called
I have a very large report file, and I want to check a certain
I have a tab-delimited text file that is very large. Many lines in the
i have a extremely large xml-file - which is derived from the field of
I have a large file where each line contains a substring such as ABC123.

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.