Skip to content
Advertisement

Compare 2 csv files and remove the common lines from 1st file | python

I want to compare 2 csv files master.csv and exclude.csv and remove all the matching lines based on column1 and write the final output in mater.csv file.

master.csv

abc,xyz
cde,fgh
ijk,lmn

exclude.csv

###Exclude list###
cde
####

Expected output (it should overwrite master.csv

abc,xyz
ijk,lmn

Tried till now

with open('exclude.csv','r') as in_file, open('master.csv','w') as out_file:
    seen = set()
    for line in in_file:
        if line in seen: continue # skip duplicate

        seen.add(line)
        out_file.write(line)

Advertisement

Answer

I believe there should be some pandas or other modules approaches, but here is a pure pythonic approach:

with open("master.csv") as f:
  master = f.read()
with open("exclude.csv") as f:
  exclude = f.read()

master = master.strip().split("n")
exclude = exclude.strip().split("n")

returnList = []
for line in master:
  check = True
  for exc in exclude:
    if exc in line:
      check = False
      break
  if check:
    returnList.append(line)

with open("master.csv", "w") as f:
  f.write("n".join(returnList))

Output of master.csv

abc,xyz
ijk,lmn
Advertisement