-1
我有兩個csv文件(),我可以把它打印出來從CSV文件總結了總數的名單。我用這個代碼:csv文件比較differnces
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
所有這一切工作正常,我只要它只是讓你可以看到我在做什麼。
所以現在我需要讓我創建了兩個新的文件之間的差異。這是我堅持的地方;我試過,但沒有運氣
diff=difflib.ndiff('file1',"rb"), ('file2',"rb")
try:
while 1:
print diff.next(),
except:
pass
我需要生成兩個新的文件之間的差異,所以我可以看到區別任何suggustions?當我運行它,我沒有錯誤,但沒有輸出
感謝
我還留着file1 = each.name,each.produce,each.amount來調用每個文件嗎?當我嘗試運行您放置的代碼時出現錯誤?對不起仍在學習 –
你會得到什麼錯誤? – mhawke
Traceback(最近一次調用最後一次): 文件「C:/ Python27/Test_energy」,第94行,在 a = open('file1','rb').read().litlines() IOError:錯誤2]沒有這樣的文件或目錄:「文件1」 –