zerocool
zerocool

Reputation: 833

pairing data between two files

I'm trying to match data between two files.

File 1:

# number of records, name
1234, keyword

File 2:

# date/time, name
2016-10-13| here is keyword in the name

as a result, I'd like to have File 3 written as:

# number of records, name, date
1234, here is keyword in the name, 2016-10-13

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 keyword in File 2.

#!/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')

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

if name in item:

is not finding any values?

Upvotes: 1

Views: 123

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140307

this

if name in item:

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

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

if any(name in cell for cell in item):

instead

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

if any(name in cell.split() for cell in item):

Upvotes: 3

Related Questions