Note that there are some explanatory texts on larger screens.

plurals
  1. POcsv file comparing differnces
    primarykey
    data
    text
    <p>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:</p> <pre><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 </code></pre> <p>All this works fine, I provided it just so you can see what I am doing.</p> <p>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</p> <pre><code> diff=difflib.ndiff('file1',"rb"), ('file2',"rb") try: while 1: print diff.next(), except: pass </code></pre> <p>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</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload