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
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.
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.