I want to make a program which would compare 2 .csv files, and print the rows that are in one of them, but not in the other. It basically works, but it stops working after performing 2 tasks in this program :/ I’m a Python beginner.
csv=open('c:\Users\***\Desktop\prvi.csv','r')
csv2=open('c:\Users\***\Desktop\drugi.csv','r')
list1=[]
choice=0
loop=0
while loop==0:
choice=input('Odaberite zeljenu funkciju: \n1)\
Usporedi Book1 sa Book2\n2) Usporedi Book2 sa Book1 \n3) Print Book1 \n4) Print Book2 \n5) Izlaz \nOdabir: ')
if choice==1:
for row in csv:
if not row in csv2:
list1.append(row)
for row in list1:
print row
del list1[0:len(list1)]
elif choice==2:
for row in csv2:
if not row in csv:
list1.append(row)
for row in list1:
print row
del list1[0:len(list1)]
elif choice==3:
for row in csv:
print row
elif choice==4:
for row in csv2:
print row
elif choice==5:
loop=1
I made an example of how to read these files, and also cleaned up your code a bit, because it hurted my eyes :D. You were using
list1in a very strange way. You put one line in it, display that line and then delete all the content of the list, which is always just one line, before moving to the next line. In your question you said that you just wanted to display the lines, so there is no need for a list. If you do need to keep track of the lines in a list, then just uselist1.append(row)directly afterprint row, without all the extra for-loop and the deleting.