Delete data in csv file using python?
<p>I have two csv files with a single column of data. How can I remove data in the second csv file in-place by comparing it with the data in the first csv file? For example: </p>

<pre><code> 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
</code></pre>

<p><a href="http://i.stack.imgur.com/TE8L8.png" rel="nofollow"><img src="http://i.stack.imgur.com/TE8L8.png" alt="File 2"></a></p>

<p>if both files are just single columns, then you could use <code>set</code> 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. </p>

<pre><code>#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)
</code></pre>

python csv