Count occurence of a word by ID in python

2024/11/14 12:42:27

Following is the content of a file,My question is how to count the number of occurences for the word "optimus" for different IDs

    ID67    DATEUID Thank you for choosing Optimus prime. Please wait for an Optimus prime to respond. You are currently number 0 in the queue. You should be connected to an agent in approximately TIMEUID.. You are now chatting with AGENTUID   0ID67    Optimus MEMORYUID Hi there! Welcome to Optimus prime Web Chat. How can I help you today?        1       ID67    Optimus DATEUID I like to pay  prepaid from CURRENCYUID with NUMBERUID expiry on whateve date. my phone no is PHONEUID 2ID12120 0 0 0 is the number. They are open 0/0 so you can ring them anytime. SMILEUID   1ID12120 Thanks Optimus, I will give them a call. Thanks for your help! HELPUID  2ID5552  is the number. They are open 0/0 so you can ring them anytime. SMILEUID 1ID5552  Thanks Optimus, I will give them a call. Thanks for your help! HELPUID  2for line in chat.txt:print line, ####print lines and count optimus word for the particular id..

Output should be like

ID67:4
ID12120
ID5552:1
Answer

One way is to use a defaultdict for the counts:

from collections import defaultdict
d = defaultdict(int)
with open("chat.txt") as f:for line in f:id, data = line.split(None, 1)d[id] += data.lower().count("optimus")
https://en.xdnf.cn/q/119713.html

Related Q&A

ModuleNotFoundError: No module named plyer in Python

I am trying to write a program notify.py (location: desktop) that uses plyer library to get a notification on windows 10. I used pip install plyer and am using vs code to run the program but I get an e…

Floating point to 16 bit Twos Complement Binary, Python

so I think questions like this have been asked before but Im having quite a bit of trouble getting this implemented. Im dealing with CSV files that contain floating points between -1 and 1. All of thes…

Flag the first non zero column value with 1 and rest 0 having multiple columns

Please assist with the belowimport pandas as pd df = pd.DataFrame({Grp: [1,1,1,1,2,2,2,2,3,3,3,4,4,4], Org1: [x,x,y,y,z,y,z,z,x,y,y,z,x,x], Org2: [a,a,b,b,c,b,c,c,a,b,b,c,a,a], Value: [0,0,3,1,0,1,0,5,…

How to split up data from a column in a csv file into two separate output csv files?

I have a .csv file, e.g.:ID NAME CATEGORIES 1, x, AB 2, xx, AA 3, xxx, BAHow would I get this to form two output .csv files based on the category e.g.:File 1:ID NAME CATEGORY 1, x, A 2, xx, A 3, …

Discord.py spellcheck commands

Recently, I looked up Stack Overflow and found this code which can check for potential typos: from difflib import SequenceMatcher SequenceMatcher(None, "help", "hepl").ratio() # Ret…

Django Model Form doesnt seem to validate the BooleanField

In my model the validation is not validating for the boolean field, only one time product_field need to be checked , if two time checked raise validation error.product_field = models.BooleanField(defau…

For loop only shows the first object

I have a code that loops through a list of mails, but it is only showing the first result, even though there are also other matches. The other results require me to loop over the mails again only to re…

IndexError: pop from empty list

I need help. I have no idea why I am getting this error. The error is in fname = 1st.pop()for i in range(num) :fname = lst.pop()lTransfer = [(os.path.join(src, fname), os.path.join(dst, fna…

Cannot import name StandardScalar from sklearn.preprocessing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

unable to solve strptime() issue even after trying all the formats

Im using the following code:data[Input_volTargetStart][1]>time.strptime(data[Dates][1], "%d %b $y")When I try to run it, I get this error:ValueError: time data 04-Jun-99 does not match for…