Python file matching and appending
<p>This is one file <code>result.csv</code>:</p>

<pre><code>M11251TH1230 
M11543TH4292 
M11435TDS144
</code></pre>

<p>This is another file <code>sample.csv</code>:</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>Can I write a Python program to compare both the files and if line in <code>result.csv</code> matches with the first word in the line in <code>sample.csv</code>, then append 1 else append 0 at every line in <code>sample.csv</code>?</p>

<p>The following snippet of code will work for you </p>

<pre><code>import csv

with open('result.csv', 'rb') as f:
    reader = csv.reader(f)
    result_list = []
    for row in reader:
        result_list.extend(row)
with open('sample.csv', 'rb') as f:
    reader = csv.reader(f)
    sample_list = []
    for row in reader:
        if row[0] in result_list:
            sample_list.append(row + [1])
        else:
            sample_list.append(row + [0]
with open('sample.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(sample_list)
</code></pre>

python append