Python: Find keywords in a text file from another text file

2024/9/22 15:37:29

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:

Invoice Date

Invoice Number

Due Date

Order Number

I am trying to find keywords from 'dict.txt' in 'invoice.txt' and then add it and the text which comes after it (but before the next keyword) in a 2 column datatable.

So it would look like :

col1 ----- col2

Invoice number ------ INV-3337

order number ---- 12345

Here is what I have done till now

with open('C:\invoice.txt') as f:invoices = list(f)with open('C:\dict.txt') as f:for line in f:dict = line.strip()for invoice in invoices:if dict in invoice:print invoice

This is working but the ordering is all wrong (it is as in dict.txt and not as in invoice.txt)

i.e. The output is

Invoice Date

Invoice Number

Due Date

Order Number

instead of the order in the invoice.txt , which is

invoice number

order number

invoice date

due date

Can you help me with how I should proceed further ?

Thank You.

Answer

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

with open('C:\invoice.txt') as f:invoice_data = [line.strip() for line in f if line.strip()] with open('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])
https://en.xdnf.cn/q/119118.html

Related Q&A

How to split a list into chucks of different sizes specified by another list? [duplicate]

This question already has answers here:How to Split or break a Python list into Unequal chunks, with specified chunk sizes(3 answers)Closed 4 years ago.I have an array I am trying to split into chunks …

Python Sum of digits in a string function

My function needs to take in a sentence and return the sum of the numbers inside. Any advice?def sumOfDigits(sentence):sumof=0for x in sentence:if sentence.isdigit(x)== True:sumof+=int(x)return sumof

How to select columns using dynamic select query using window function

I have sample input dataframe as below, but the value (clm starting with m) columns can be n number. customer_id|month_id|m1 |m2 |m3 .......m_n 1001 | 01 |10 |20 1002 | 01 |20…

Downloading Books from website with python

Im downloading books from the website, and almost my code runs smoothly, but when I try to open the pdf Book on my PC. An error generated by Adobe Acrobat Reader that this is not supported file type.He…

Discord.py How can I make a bot delete messages after a specific amount of time

I have a discord bot that sends images to users when they use the !img command, I dont want people to request an image and then have it sit there until someone deletes it. Is there any way I can make i…

How to encode and decode a column in python pandas?

load = pd.DataFrame({A:list(abcdef),B:[4,5,4,5,5,4],C:[7,8,9,4,2,0],D:[1,3,5,4,2,0],E:[5,3,6,9,2,4],F:list(aaabbb)})How to encode and decode column F.Expected Output:Should have two more columns with e…

Pygame module not found [duplicate]

This question already has answers here:Why do I get a "ModuleNotFoundError" in VS Code despite the fact that I already installed the module?(23 answers)Closed 3 months ago.I have installed p…

Working with Lists and tuples

My data looks like:X=[1,2,3,4]But I need it to look like: Y=[(1,2,3,4)]How does one do this in python?

Football pygame, need help on timer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Unable to Install GDAL Using PIP on Python

Im trying to install gdal in python3.8.8 (Windows 10) and im getting below error I have install Visual Studio Build Tools 2019 and reboot my PC Downgrade my Python from 3.9.5 to 3.8.8 C:\Program Files…