w3resource

Python: Count the number of each character of a given text of a text file

Python Basic - 1: Exercise-7 with Solution

Write a Python program to count the number of each character in a text file.

Inputs:
abc.txt -
German Unity Day
From Wikipedia, the free encyclopedia
The Day of German Unity (German: Tag der DeutschenEinheit) is the national day of Germany, celebrated on 3 October as a public holiday. It commemorates the anniversary of German reunification in 1990, when the goal of a united Germany that originated in the middle of the 19th century, was fulfilled again. Therefore, the name addresses neither the re-union nor the union, but the unity of Germany. The Day of German Unity on 3 October has been the German national holiday since 1990, when the reunification was formally completed.

Sample Solution:

Python Code :

# Import necessary modules.
import collections  # Import the 'collections' module for Counter.
import pprint  # Import 'pprint' for pretty printing.

# Get the file name as input from the user.
file_input = input('File Name: ')

# Open the file in read mode using a 'with' statement to ensure proper handling of resources.
with open(file_input, 'r') as info:
    # Read the contents of the file and count the occurrences of each uppercase character.
    count = collections.Counter(info.read().upper())
    # Use 'pprint' to format the count output for better readability.
    value = pprint.pformat(count)

# Print the formatted count of characters in the file.
print(value)

Sample Output:

File Name:  abc.txt
Counter({' ': 93,
         'E': 64,
         'N': 45,
         'A': 42,
         'T': 40,
         'I': 36,
         'O': 31,
         'R': 29,
         'H': 25,
         'D': 19,
         'M': 17,
         'Y': 17,
         'L': 15,
         'F': 15,
         'U': 14,
         'C': 13,
         'G': 13,
         'S': 12,
         ',': 7,
         'B': 6,
         'W': 5,
         '9': 5,
         '.': 4,
         'P': 4,
         '1': 3,
         '\n': 2,
         '0': 2,
         '3': 2,
         ':': 1,
         '-': 1,
         'K': 1,
         '(': 1,
         ')': 1,
         'V': 1})

Explanation:

The above code takes a filename as input from the user. It reads the file content, converts it to uppercase, and then counts the occurrences of each uppercase character. The results are pretty-printed using the 'pprint' module in Python's standard library.

Flowchart:

Flowchart: Python - Count the number of each character of a  text file

Python Code Editor :

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print a long text, convert the string to a list and print all the words and their frequencies.
Next: Write a Python program to get the top stories from Google news.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/basic/python-basic-1-exercise-7.php