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 collections
import pprint
file_input = input('File Name: ')
with open(file_input, 'r') as info:
  count = collections.Counter(info.read().upper())
  value = pprint.pformat(count)
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})

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.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Kwargs:

**kwargs and *args are function arguments that can be very useful.

They are quite underused and often under-understood as well.

Let's try to explain what kwargs are and how to use them.

  • While *args are used to pass arguments at an unknown amount to functions, **kwargs are used to do the same but with named arguments.
  • So, if *args is a list being passed as an argument, you can think of **kwargs as a dictionary that's being passed as an argument to a function.
  • You can use arguments as you wish as long as you follow the correct order which is: arg1, arg2, *args, **kwargs. It's okay to use only one of those but you can't mix the order, for instance, you can't have: function(**kwargs, arg1), that'd be a major faux pas in Python.
  • Another example: You can do function(*args,**kwargs) since it follows the correct order.
  • Here is an example. Let's say satelites are given with their names and weight in tons in dictionary format. Code prints their weight as kilograms along with their names.
def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*100)
payloads(NavSat1 = '2.5', BaysatG2 = '4')

Output:

NavSat1 ||| 250.0
BaysatG2 ||| 400.0

Since the function above would work for any number of dictionary keys, **kwargs makes perfect sense rather than passing arguments with a fixed amount.

def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*100)

sats={"Tx211":"3", "V1":"0.50"}
payloads(**sats)

Output:

Tx211 ||| 300.0
V1 ||| 50.0