Given a text file that contains the key-Value pair as data the task is to print the Count of each key-value pair using Python.
Program to Count the Number of Occurrence of Key-Value Pair in a Text File in Python
Approach:
- Import the Counter from the collections module using the import keyword.
-
Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
-
Take an empty list to store all the words.
-
Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
-
Iterate through the lines of the file using the For loop.
-
Remove the newline character (\n) using strip() function and store it in a variable.
- Inside the For loop append each key-value pair to list using the append() function.
-
Get the frequency of all the key-value pairs using the Counter() function and store it in a variable(of type dictionary).
-
Traverse in the frequency dictionary using the For loop.
-
Print its corresponding key(frequency).
Below is the implementation:
# import the Counter from collections module using the import keyword from collections import Counter # Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Take a empty list to store all the words l = [] print('The Count of frequency of each key-value pair is :') # Open the file in read-only mode. In this case, we're simply reading the contents of the file. with open(givenFilename, 'r') as givenfilecontent: # Iterate through the lines of the file using the For loop. for gvnfileline in givenfilecontent: # Remove the newline character (\n) using strip() function and store it in a variable. keyval = gvnfileline.strip('\n') # Inside the For loop append each key-value pair to list using the append() function. l.append(keyval) # Get frequency of all the words using the Counter() function and store it in a variable(of type dictionary) freqword = Counter(l) # Traverse in the frequency dictionary using the For loop for key in freqword: # print its corresponding key(frequency) print(key, '->', freqword[key])
Output:
The Count of frequency of each key-value pair is : btechgeeks:500 -> 3 hello:200 -> 2 good:900 -> 1
Samplefile.txt:
btechgeeks:500 hello:200 hello:200 good:900 btechgeeks:500 btechgeeks:500
- Python Program to Count Words in a Text File those are Ending with Alphabet “e”
- Python Program to Count Number of Digits in a Text File
- Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix
Explanation:
- The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
- Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
- The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
- When we open the file, we use the character ‘r’ to signify read-mode.
- strip() function removes the newline character.
- We apply the Counter() function to get the frequency of all the key-value pairs of the given text file.
Sample Implementation in google colab: