Given a text file containing several key-value pairs in the format key = value, find how many times a specific key-value pair occurs in the file. For Example:
Input file: name=Jennie
age=25
name=Jennie
city=New York
name=Lisa
Input: key=name, value= Jennie
Output: Occurrences of 'name=Jennie' : 2
Below is a sample text file used in this article:

Now, Let's explore different methods to count the number of occurrences of a key value pair in a text file.
Using collections.Counter
This method uses Python’s Counter to easily count how many times each key-value pair appears in the file.
from collections import Counter
k = "name"
v = "Harry"
with open("file.txt", "r") as f:
lines = [line.strip() for line in f if line.strip()]
counts = Counter(lines)
target = k + "=" + v
print("Occurrences of '" + target + "':", counts.get(target, 0))
Output
Occurrences of 'name=Harry': 3
Explanation:
- [line.strip() for line in f if line.strip()]: Reads all lines, removes whitespace, ignores empty lines.
- Counter(lines): Counts occurrences of each line.
- counts.get(target,0): Retrieves the count of the specific key-value pair, defaults to 0 if not present.
Using a Manual Counter
This method reads the file line by line and increments a counter whenever the target key-value pair is found.
k = "name"
v = "Harry"
count = 0
with open("file.txt", "r") as f:
for line in f:
if line.strip() == k + "=" + v:
count = count + 1
print("Occurrences of '" + k + "=" + v + "':", count)
Output
Occurrences of 'name=harry': 3
Explanation:
- for line in f: Reads the file line by line.
- line.strip() == k + "=" + v: Checks if the cleaned line matches the target key–value pair.
- count = count + 1: Increments the count when a match is found.
Using Regular Expressions
This method uses the re module to find all exact matches of the key-value pair in the file.
import re
k = "name"
v = "Harry"
with open("file.txt", "r") as f:
data = f.read()
pattern = r"\b" + re.escape(k) + "=" + re.escape(v) + r"\b"
matches = re.findall(pattern, data)
print("Occurrences of '" + k + "=" + v + "':", len(matches))
Output
Occurrences of 'name=Harry': 3
Explanation:
- f.read(): Reads the whole file as a single string.
- re.escape(k) / re.escape(v): Ensures special characters are treated literally.
- pattern = r"\b" + re.escape(k) + "=" + re.escape(v) + r"\b": Builds a regex to match the exact key–value pair.
- re.findall(pattern, data): Finds all matches; len(matches) gives the total count.
Using readlines() + Loop
This method reads all lines of the file into a list and then loops through them to count the key-value pair. It’s simple but not memory-efficient for large files.
k = "name"
v = "Harry"
count = 0
with open("file.txt", "r") as f:
lines = f.readlines()
for line in lines:
if line.strip() == k + "=" + v:
count = count + 1
print("Occurrences of '" + k + "=" + v + "':", count)
Output
Occurrences of 'name=Harry': 3
Explanation:
- f.readlines(): Reads all lines into a list.
- Loop checks each line for the target key-value pair and increments count.