Python program to count the number of blank spaces in a text file
Last Updated :
26 Nov, 2020
All programs need input to process and after processing gives the output. Python support file handling and allow user to handle files. The concept of file handling has stretched over various other languages, But the implementation is either lengthy or complicated. Python treats file differently as text and binary,
One thing to note while writing data to a file is that its consistency and integrity should be maintained. Once you have stored your data on a file now the most important thing is its retrieval because the computer data store as bits of 1s and 0s and if its retrieval is not done properly then it becomes completely useless and data is said to be corrupted. Hence, writing as well as reading is also an important aspect of File Handling in Python.
How to count the number of blank spaces or any character?
' ' (Space) also comes under Printable ASCII Character type, while NULL is not Printable ASCII Character type.
Approach :
How to write to a file using Python?
- Open a file to write.
- Count the number of spaces in that text file.
- Close a file.
Here our text file.
Implementation:
Methods #1: Using isspace() function.
Firstly, we will open our text file and store that text file in that variable. A loop is used to count spaces in a text file. if condition ( char.isspace()) to test all the condition, if it returns True then the count will be incremented by 1. After testing all the character's loop will return False and terminate itself. Finally, the program will display the total number of spaces.
Python3
# this will open the file and store
# in "file" variable
file = open("test1.txt", "r")
count = 0
while True:
# this will read each character
# and store in char
char = file.read(1)
if char.isspace():
count += 1
if not char:
break
print(count)
Output:
5
Note - isspace() also count new line character therefore it is showing output as 6.
Methods #2: Using Loop:
Firstly, we will open our text file and store that text file in that variable. A loop is used to count spaces in a text file. if condition ( char == " " ) to test all the condition, if it returns True then the count will be incremented by 1. After testing all the character's loop will return False and terminate itself. Finally, the program will display the total number of spaces.
Python3
# this will open the file and store in
# "file" variable
file = open("test1.txt", "r")
count = 0
while True:
# this will read each character
# and store in char
char = file.read(1)
if char == " ":
count += 1
if not char:
break
print(count)
Output:
4
Methods #3: Using a python module "functools".
A Partial function is a function for a particular argument value. They can be created in python by using 'partial' from the "functools" library.
Python3
import functools
with open("test1.txt") as file:
file_char = functools.partial(file.read, 1)
for char in iter(file_char, " "):
if char == " ":
count += 1
print("count: ", count)
Output:
4
Similar Reads
Python program to count the number of spaces in string In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or stringPythons = "Count the spaces in this string." # Count spaces using the count() method space_co
3 min read
Python program to Count the Number of occurrences of a key-value pair in a text file Given a text file of key-value pairs. The task is to count the number of occurrences of the key-value pairs in the file with PythonProgram to Count the occurrences of a key-value pairNaive Approach to Count the Occurrences of a key-value PairUsing Python built-in collections.CounterUsing regular exp
3 min read
Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Python Program to Find the Number of Unique Words in Text File Given a text file, write a python program to find the number of unique words in the given text file in Python.Examples:Input: gfg.txtOutput: 18Contents of gfg.txt: GeeksforGeeks was created with a goal in mind to provide well written well thought and wellexplained solutions for selected questionsExp
2 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read
Get number of characters, words, spaces and lines in a file - Python Given a text file fname, the task is to count the total number of characters, words, spaces, and lines in the file. As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to calculate the total number of characters, words, spaces, and line
5 min read