Python Example – Write a Python program to count the number of each character of a text file

(Python Example for Citizen Data Scientist & Business Analyst)

 

Write a Python program to count the number of each character of 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})

 

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

 

Sign up to get end-to-end “Learn By Coding” example.


 

 

Applied Machine Learning & Data Science with Python, R and SQL.

A list of Python, R and MATLAB Codes for Applied Machine Learning and Data Science https://setscholars.net/

 

Subscribe to our YouTube channel and press the ring to get latest update on Python and R coding recipesCoding is our strength.

 

Learn by Coding Categories:

Classification: https://setscholars.net/category/classification/

Data Analytics: https://setscholars.net/category/data-analytics/

Data Science: https://setscholars.net/category/data-science/

Data Visualisation: https://setscholars.net/category/data-visualisation/

Machine Learning Recipe: https://setscholars.net/category/machine-learning-recipe/

Pandas: https://setscholars.net/category/pandas/

Python: https://setscholars.net/category/python/

SKLEARN: https://setscholars.net/category/sklearn/

Supervised Learning: https://setscholars.net/category/supervised-learning/

Tabular Data Analytics: https://setscholars.net/category/tabular-data-analytics/

End-to-End Data Science Recipes: https://setscholars.net/category/a-star-data-science-recipe/

Applied Statistics: https://setscholars.net/category/applied-statistics/

Bagging Ensemble: https://setscholars.net/category/bagging-ensemble/

Boosting Ensemble: https://setscholars.net/category/boosting-ensemble/

CatBoost: https://setscholars.net/category/catboost/

Clustering: https://setscholars.net/category/clustering/

Data Analytics: https://setscholars.net/category/data-analytics/

Data Science: https://setscholars.net/category/data-science/

Data Visualisation: https://setscholars.net/category/data-visualisation/

Decision Tree: https://setscholars.net/category/decision-tree/

LightGBM: https://setscholars.net/category/lightgbm/

Machine Learning Recipe: https://setscholars.net/category/machine-learning-recipe/

Multi-Class Classification: https://setscholars.net/category/multi-class-classification/

Neural Networks: https://setscholars.net/category/neural-networks/

Python Machine Learning: https://setscholars.net/category/python-machine-learning/

Python Machine Learning Crash Course: https://setscholars.net/category/python-machine-learning-crash-course/

R Classification: https://setscholars.net/category/r-classification/

R for Beginners: https://setscholars.net/category/r-for-beginners/

R for Business Analytics: https://setscholars.net/category/r-for-business-analytics/

R for Data Science: https://setscholars.net/category/r-for-data-science/

R for Data Visualisation: https://setscholars.net/category/r-for-data-visualisation/

R for Excel Users: https://setscholars.net/category/r-for-excel-users/

R Machine Learning: https://setscholars.net/category/r-machine-learning/

R Machine Learning Crash Course: https://setscholars.net/category/r-machine-learning-crash-course/

R Regression: https://setscholars.net/category/r-regression/

Regression: https://setscholars.net/category/regression/

XGBOOST: https://setscholars.net/category/xgboost/

Excel examples for beginners: https://setscholars.net/category/excel-examples-for-beginners/

C Programming tutorials & examples: https://setscholars.net/category/c-programming-tutorials/

Javascript tutorials & examples: https://setscholars.net/category/javascript-tutorials-and-examples/

Python tutorials & examples: https://setscholars.net/category/python-tutorials/

R tutorials & examples: https://setscholars.net/category/r-for-beginners/

SQL tutorials & examples: https://setscholars.net/category/sql-tutorials-for-business-analyst/

SQL for Beginners in 2 Weeks: https://setscholars.net/category/sql-for-citizen-data-scientist-in-2-weeks/

 

Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R @ https://wacamlds.podia.com/portfolio-projects-for-aspiring-data-scientists-end-to-end-applied-machine-learning-solutions-in-python-r

 

 


Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.