Hi, I need help with this python programming question. Based on the picture abov
ID: 3913106 • Letter: H
Question
Hi, I need help with this python programming question. Based on the picture above, I need to upload 2 files (old.csv and new.csv) then make an output called with difference.csv. The output has to be exactly the same thing as difference.csv like the picture above, the specification are:
1. Keep the header (name, city)
2. Add a "Report" column and mark every row with the changes/addition/deletion
3. Create difference.csv exactly the same as the picture shown above
Thanks a lot!
None ity anuato dChenges Adet deleton Ordon Shone NYC asudena asadin& tonnu cl r i hineyc tOsExplanation / Answer
here is your Code : ------------->>>>>>>>
oldName = []
oldCity = []
newName = []
newCity = []
with open("old.csv") as f:
for line in f:
s = line.split(" ")
sst = str(s[0]).split(",")
oldName.append(sst[0])
oldCity.append(sst[1])
with open("new.csv") as f:
for line in f:
s = line.split(" ")
sst = str(s[0]).split(",")
newName.append(sst[0])
newCity.append(sst[1])
diffName = []
diffCity = []
diffReport = []
diffName.append(oldName[0])
diffCity.append(oldCity[0])
diffReport.append("Report")
#for finding the difference
for i in range(1,len(oldName)):
st = 0
for j in range(1,len(newName)):
if oldName[i] == newName[j]:
st = 1
if oldCity[i] != newCity[j]:
diffName.append(oldName[i])
diffCity.append(newCity[j])
diffReport.append("Changed")
break
if st == 0:
diffName.append(oldName[i])
diffCity.append(oldCity[i])
diffReport.append("Deletion")
for i in range(0,len(newName)):
st = 0
for j in range(0,len(oldName)):
if newName[i] == oldName[j]:
st = 1
break
if st == 0:
diffName.append(newName[i])
diffCity.append(newCity[i])
diffReport.append("Addition")
fo = open("difference.csv","w")
for i in range(0,len(diffName)):
st = str(diffName[i])+","+str(diffCity[i])+","+str(diffReport[i])+" "
fo.write(st)
fo.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.