file writing not working as expected
<p>I have a python code where it will take the first column of a sample.csv file and copy it to temp1.csv file. Now I would like to compare this csv file with another serialNumber.txt file for any common rows. If any common rows found, It should write to a result file. My temp1.csv is being created properly but the problem is the result file which is being created is empty.</p>

<p>script.py</p>

<pre><code>import csv
f = open("sample.csv", "r")
reader = csv.reader(f)

data = open("temp1.csv", "wb")
w = csv.writer(data)
for row in reader:
    my_row = []
    my_row.append(row[0])
    w.writerow(my_row)
data.close()

with open('temp1.csv', 'r') as file1:
    with open('serialNumber.txt', 'r') as file2:
        same = set(file1).intersection(file2)
        print same

with open('result.csv', 'w') as file_out:
    for line in same:
        file_out.write(line)
        print line
</code></pre>

<p>sample.csv</p>

<pre><code>M11435TDS144,STB#1,Router#1 
M11543TH4292,STB#2,Router#1 
M11509TD9937,STB#3,Router#1
M11543TH4258,STB#4,Router#1
</code></pre>

<p>serialNumber.txt</p>

<pre><code>G1A114042400571
M11543TH4258
M11251TH1230
M11435TDS144
M11543TH4292
M11509TD9937
</code></pre>

<p>You probably want this instead</p>

<pre><code>same = set(list(file1)).intersection(list(file2))
</code></pre>

python file-writing