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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:16:59+00:00 2026-06-01T08:16:59+00:00

I have written a script which works, but I’m guessing isn’t the most efficient.

  • 0

I have written a script which works, but I’m guessing isn’t the most efficient. What I need to do is the following:

  • Compare two csv files that contain user information. It’s essentially a member list where one file is a more updated version of the other.
  • The files contain data such as ID, name, status, etc, etc
  • Write to a third csv file ONLY the records in the new file that either don’t exist in the older file, or contain updated information. For each record, there is a unique ID that allows me to determine if a record is new or previously existed.

Here is the code I have written so far:

import csv

fileAin = open('old.csv','rb')
fOld = csv.reader(fileAin)

fileBin = open('new.csv','rb')
fNew = csv.reader(fileBin)

fileCout = open('NewAndUpdated.csv','wb')
fNewUpdate = csv.writer(fileCout)

old = []
new = []

for row in fOld:
    old.append(row)
for row in fNew:
    new.append(row)

output = []

x = len(new)
i = 0
num = 0

while i < x:
    if new[num] not in old:
        fNewUpdate.writerow(new[num])

    num += 1
    i += 1

fileAin.close()
fileBin.close()
fileCout.close()

In terms of functionality, this script works. However I’m trying to run this on files that contain hundreds of thousands of records and it’s taking hours to complete. I am guessing the problem lies with reading both files to lists and treating the entire row of data as a single string for comparison.

My question is, for what I am trying to do is this there a faster, more efficient, way to process the two files to create the third file containing only new and updated records? I don’t really have a target time, just mostly wanting to understand if there are better ways in Python to process these files.

Thanks in advance for any help.

UPDATE to include sample row of data:

123456789,34,DOE,JOHN,1764756,1234 MAIN ST.,CITY,STATE,305,1,A

  • 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-01T08:17:02+00:00Added an answer on June 1, 2026 at 8:17 am

    How about something like this? One of the biggest inefficiencies of your code is checking whether new[num] is in old every time because old is a list so you have to iterate through the entire list. Using a dictionary is much much faster.

    import csv
    
    fileAin = open('old.csv','rb')
    fOld = csv.reader(fileAin)
    
    fileBin = open('new.csv','rb')
    fNew = csv.reader(fileBin)
    
    fileCout = open('NewAndUpdated.csv','wb')
    fNewUpdate = csv.writer(fileCout)
    
    old = {row[0]:row[1:] for row in fOld}
    new = {row[0]:row[1:] for row in fNew}
    fileAin.close()
    fileBin.close()
    
    output = {}
    
    for row_id in new:
        if row_id not in old or not old[row_id] == new[row_id]:
            output[row_id] = new[row_id]
    
    for row_id in output:
        fNewUpdate.writerow([row_id] + output[row_id])
    
    
    fileCout.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script which works fine in PHP 5, but not in
I'm using emacs and I have written a script which uses current-buffer. However the
I have written a ruby script which opens up dlink admin page in firefox
I have written a python script which watches a directory for new subdirectories, and
I have written a news scroller which uses the Prototype and Script.aculo.us libraries to
This is the script I have written for gzipping content on my site, which
I have written the following Perl script- use HTML::TreeBuilder; my $html = HTML::TreeBuilder->new_from_content(<<END_HTML); <span
I have a search suggestion script written in jQuery which uses the jQuery framework
I have written a PHP script which allows me to modify and update a
I have a php script written which calls an external command using exec which

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.