-1
두 개의 CSV 파일()이 있습니다. CSV 파일에서 합계 된 합계를 사용하여 목록을 인쇄 할 수 있습니다. 나는이 코드를 사용한다 :차이점을 비교하는 CSV 파일
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
이 모든 것이 잘 작동하는데, 나는 이것이 내가하는 일을 볼 수 있도록 제공했다.
그래서 지금 내가 만든 두 개의 새 파일의 차이점을 알아야합니다. 이것이 내가 붙어있는 곳이다. 나는이 행운을 시험해 보았다.
차이점을 볼 수 있도록 생성되는 두 개의 새 파일의 차이점이 필요합니까? 내가 그것을 실행할 때 나는 오류를 얻을 있지만 출력
감사
각 파일을 호출하기 위해 file1 = each.name, each.produce, each.amount를 그대로 두시겠습니까? 내가 넣은 코드를 실행하려고하면 오류가 발생합니까? 죄송합니다 아직 배우는 –
어떤 오류가 발생합니까? – mhawke
에있는 "C :/Python27/Test_energy"파일에있는 파일을 추적 (가장 최근에 마지막으로 호출) :IOError : [파일 : Errno 2] No such file or directory : 'file1' –