pairing data between two files
<p>I'm trying to match data between two files.</p>

<p>File 1:</p>

<pre class="lang-none prettyprint-override"><code># number of records, name
1234, keyword
</code></pre>

<p>File 2:</p>

<pre class="lang-none prettyprint-override"><code># date/time, name
2016-10-13| here is keyword in the name
</code></pre>

<p>as a result, I'd like to have File 3 written as:</p>

<pre class="lang-none prettyprint-override"><code># number of records, name, date
1234, here is keyword in the name, 2016-10-13
</code></pre>

<p>So the idea here is to iterate through File 1, get all keywords and check File 2 if it exists there. If that's true, take first field (date) from File 2 and put is as last item in File 1. I wrote following code to test few things but it's not working well. First issue I have is that python is not finding any <code>keyword</code> in File 2.</p>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv

FILE1='file1'
FILE2='file2'

file2data=[]
with open(FILE2, 'rb') as file2file:
    reader = csv.reader(file2file, delimiter='|', quotechar='"')
    for row in reader:
        file2data.append(row)

def check(name):
    print('checking: "%s"' % name)
    rval=[]
    for item in file2data:
        if name in item:
            rval.append(item)
    return rval

with open(FILE1, 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in csvreader:
        entries=row[0]
        keyword=row[1].strip()
        checked=check(keyword)
        if checked:
            print('ok')
</code></pre>

<p>Anyone know why is that happening? Why inside <code>check()</code> function the following code</p>

<pre><code>if name in item:
</code></pre>

<p>is not finding any values?</p>

<p>this</p>

<pre><code>if name in item:
</code></pre>

<p>checks if there is an item <em>cell</em> with the exact content <code>name</code> in the <code>item</code> row (list of cells) (<code>item</code> is actually the row you stored earlier, bad naming :))</p>

<p>What you need is to scan each item to see if the string is contained. So write:</p>

<pre><code>if any(name in cell for cell in item):
</code></pre>

<p>instead</p>

<p><code>any</code> will return <code>True</code> as soon as if finds <code>name</code> substring in a cell of <code>item</code>.
Note that this is a substring match, not a word match. <code>key</code> will match a string containing <code>keyword</code>. If you want a word match (by splitting words according to blanks):</p>

<pre><code>if any(name in cell.split() for cell in item):
</code></pre>

python