Comparing two columns of a csv and outputting string similarity ratio in another csv
<p>I am very new to python programming. I am trying to take a csv file that has two columns of string values and want to compare the similarity ratio of the string between both columns. Then I want to take the values and output the ratio in another file. </p>

<p>The csv may look like this:</p>

<pre><code>Column 1|Column 2 
tomato|tomatoe 
potato|potatao 
apple|appel 
</code></pre>

<p>I want the output file to show for each row, how similar the string in Column 1 is to Column 2. I am using difflib to output the ratio score.</p>

<p>This is the code I have so far:</p>

<pre><code>import csv
import difflib

f = open('test.csv')

csf_f = csv.reader(f)

row_a = []
row_b = []

for row in csf_f:
    row_a.append(row[0])
    row_b.append(row[1])

a = row_a
b = row_b

def similar(a, b):
    return difflib.SequenceMatcher(a, b).ratio()

match_ratio = similar(a, b)

match_list = []
for row in match_ratio:
    match_list.append(row)

with open("output.csv", "wb") as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerows(match_list)

f.close()
</code></pre>

<p>I get the error:</p>

<pre><code>Traceback (most recent call last):
  File "comparison.py", line 24, in &lt;module&gt;
    for row in match_ratio:
TypeError: 'float' object is not iterable
</code></pre>

<p>I feel like I am not importing the column list correctly and running it against the sequencematcher function.</p>