Skip to content Skip to sidebar Skip to footer

Python: Find Keywords In A Text File From Another Text File

Take this invoice.txt for example Invoice Number INV-3337 Order Number 12345 Invoice Date January 25, 2016 Due Date January 31, 2016 And this is what dict.txt looks like: Invoic

Solution 1:

This should work. You can load your invoice data into a list, and your dict data into a set for easy lookup.

withopen('C:\invoice.txt') as f:
    invoice_data = [line.strip() for line in f if line.strip()] 

withopen('C:\dict.txt') as f:
    dict_data = set([line.strip() for line in f if line.strip()])

Now iterate over invoices, 2 at a time and print out the line sets that match.

for i in range(0, len(invoice_data), 2):
    if invoice_data[i] in dict_data:
        print(invoive_data[i: i + 2])

Post a Comment for "Python: Find Keywords In A Text File From Another Text File"