bado_91 bado_91 - 3 months ago 11
Python Question

Delete data in csv file using python?

I have two csv files in forme of one column,and i want to remove the existing line in file2 from file1.

import csv
reader1 = csv.reader(open("file1.csv", "rb"))
reader = csv.reader(open("file2.csv", "rb"))f
for line in reader:
if line in reader1:
print line


File 2

Answer

if both files are just single columns, then you could use set to remove the differences. However, this presumes that the entries in each file do not need to be duplicated and their order doesn't really matter.

#since each file is a column, unroll each file into a single list:
dat1 = [x[[0]] for x in reader1]
dat2 = [y[[0]] for y in reader]

#take the set difference
dat1_without_dat2 = set(dat1).difference(dat2)
Comments