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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:53:11+00:00 2026-06-05T21:53:11+00:00

I have two csv file ( ) I can get it to print out

  • 0

I have two csv file ( ) I can get it to print out the the list with the totals summed up from the csv files. i use this code:

 import csv 
 import difflib

 file  = open('test1.csv',"rb") #Open CSV File in Read Mode 
 reader = csv.reader(file)      #Create reader object which iterates over lines 

 class Object:                   #Object to store unique data 
  def __init__(self, name, produce, amount): 
    self.name = name 
    self.produce = produce 
    self.amount = amount 

 rownum = 0 #Row Number currently iterating over 
 list = []  #List to store objects 

 def checkList(name, produce, amount): 

 for object in list:  #Iterate through list         
    if object.name == name and object.produce == produce:  #Check if name and produce        combination exists 
        object.amount += int(amount) #If it does add to amount variable and break out 
        return 

newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount 
list.append(newObject)  #Add to list and break out 


 for row in reader:  #Iterate through all the rows 
  if rownum == 0:  #Store header row seperately to not get confused 
    header = row 
else: 
    name = row[0]  #Store name 
    produce = row[1]  #Store produce 
    amount = row[2]  #Store amount 

    if len(list) == 0:  #Default case if list = 0 
        newObject = Object(name, produce, int(amount)) 
        list.append(newObject) 
    else:  #If not... 
        checkList(name, produce, amount) 


rownum += 1 

 for each in list: 
  file1 =  each.name, each.produce, each.amount #END OF FILE 1


 file  = open('test2.csv',"rb") #Open CSV File in Read Mode 
 reader = csv.reader(file)      #Create reader object which iterates over lines 

 class Object:                   #Object to store unique data 
  def __init__(self, name, produce, amount): 
    self.name = name 
    self.produce = produce 
    self.amount = amount 

 rownum = 0 #Row Number currently iterating over 
 list = []  #List to store objects 

 def checkList(name, produce, amount): 

  for object in list:  #Iterate through list         
    if object.name == name and object.produce == produce:  #Check if name and produce    combination exists 
        object.amount += int(amount) #If it does add to amount variable and break out 
        return 

newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount 
list.append(newObject)  #Add to list and break out 


 for row in reader:  #Iterate through all the rows 
  if rownum == 0:  #Store header row seperately to not get confused 
    header = row 
else: 
    name = row[0]  #Store name 
    produce = row[1]  #Store produce 
    amount = row[2]  #Store amount 

    if len(list) == 0:  #Default case if list = 0 
        newObject = Object(name, produce, int(amount)) 
        list.append(newObject) 
    else:  #If not... 
        checkList(name, produce, amount) 


rownum += 1 

 for each in list: 
   file2 = each.name, each.produce, each.amount #END OF FILE 2

All this works fine, I provided it just so you can see what I am doing.

So now I need to get the difference between the two new files I created. this is where I ger stuck; I tried this but no luck

 diff=difflib.ndiff('file1',"rb"), ('file2',"rb")
 try:
    while 1:
        print diff.next(),

 except:
     pass

I need the difference between the two new files to be generated so I can see the difference any suggustions? when i run it I get no errors, but no output

  • 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-05T21:53:13+00:00Added an answer on June 5, 2026 at 9:53 pm

    EDIT: Given your earlier question, it would seem that you should already know what is wrong.

    Firstly you need to use the correct number of brackets.

    diff = difflib.ndiff(('file1', 'rb'), ('file2', 'rb'))
    

    But this is still incorrect because difflib.ndiff requires two lists of strings, not the names and modes of unopened files. You need to read the contents of the files into a list of lines.

    a = open('file1', 'rb').read().splitlines()
    b = open('file2', 'rb').read().splitlines()
    
    for diff in difflib.ndiff(a, b):
        print diff
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two .csv files containing correlation matrices exported from R. One file contains
I have two CSV files which use @ to divide each column. The first
I have two long list, one from a log file that contains lines formatted
I have the following PHP script which reads from two CSV files, At the
I have two csv file. First File has date offerid clicks orders Second File
I have two files client.php and server.php. The client file send a HTTP request
I have a dataframe in R that I loaded from a CSV file. One
I have a CSV file, formatted like this: 0001 @ word @ some information
I have a data table I've loaded from a CSV file. I need to
In Python, I have an application that writes a list to a CSV file,

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.