Reputation: 41
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
Upvotes: 0
Views: 1028
Reputation: 2664
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 list1
in 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 use list1.append(row)
directly after print row
, without all the extra for-loop and the deleting.
# open lines as list, using read().split('\n') instead of readlines(),
# because readlines() leaves '\n' after the lines.
csv = open(r'c:\Users\***\Desktop\prvi.csv', 'r').read().split('\n')
csv2 = open(r'c:\Users\***\Desktop\drugi.csv', 'r').read().split('\n')
list1 = []
while True:
# never use input(), unless you are using python 3
choice = int(raw_input('Odaberite zeljenu funkciju: \n'
'1) Usporedi Book1 sa Book2\n'
'2) Usporedi Book2 sa Book1 \n'
'3) Print Book1 \n'
'4) Print Book2 \n'
'5) Izlaz \nOdabir: '))
if choice == 1:
for row in csv:
if not row in csv2:
list1.append(row)
print row
save_file()
elif choice == 2:
for row in csv2:
if not row in csv:
list1.append(row)
print row
save_file()
elif choice == 3:
for row in csv:
print row
elif choice == 4:
for row in csv2:
print row
elif choice == 5:
break
def save_file():
with open('output.txt', 'w') as f:
f.write('\n'.join(list1))
Upvotes: 0
Reputation: 186
If there are no duplicates in your csv files which have to be retained in the output, I'd use sets here and simply let Python compute the difference.
difference(other, ...)
set - other - ...
Return a new set with elements in the set that are not in the others.
csv = set(open(r'c:\Users\***\Desktop\prvi.csv', 'r').read().split('\n'))
csv2 = set(open(r'c:\Users\***\Desktop\drugi.csv', 'r').read().split('\n'))
#...
# get everything in csv1, but not in csv2
diff1 = csv1 - csv2
# get everything in csv2, but not in csv1
diff2 = csv2 - csv1
Upvotes: 0
Reputation: 399753
You probably need to rewind or re-open the files, since after iterating over the contents once, the files are "exhausted".
Upvotes: 2