Practice Question 24
In this question, we will learn how to write a function that takes a “messy” file containing multiple spaces between words and cleans it up so that every word is separated by exactly one single space.
The Logic (How it works)
Before writing the code, let us first understand the logic behind the solution.
The “Secret Weapon” here is the .split() method.
-
The Magic of
.split(): When you call.split()without any arguments, Python automatically ignores all consecutive whitespace (spaces, tabs, newlines) and just extracts the words. -
The List of Words: This gives us a clean list of words with no spaces included.
-
The Join Strategy: We then use
" ".join(wordlist)to put the words back together into a single string, with exactly one space between each word. -
Write to File: Finally, we save this “clean” string into a new file.
Let’s Code as per the above logic :
def removeExtraSpaces():
f1 = open("data.txt", "r")
content = f1.read()
f1.close()
wordlist = content.split()
clean_content = " ".join(wordlist)
f2 = open("clean.txt", "w")
f2.write(clean_content)
f2.close()
print("Extra spaces removed! Cleaned content saved to clean.txt")
# To call the function:
removeExtraSpaces()
Important Notes for Students
As a teacher, I want you to pay close attention to these points, as they are where most students make mistakes in exams:
-
Why not use
.replace()? If you have five spaces and replace" "with" ", you still have three spaces left. You would have to run a loop many times to get it right..split()is much more efficient. -
Handling Newlines: Be aware that
" ".join(wordlist)will turn the whole file into one single line. If you need to keep the original paragraph structure, you should apply this logic line-by-line inside areadlines()loop. -
Data Normalization: This process is called “Whitespace Normalization” and is a very common first step in data science and text analysis.
Teacher’s Secret Tip for Exams:
If the examiner asks you to remove all spaces (like a password generator), use "".join(wordlist) (with no space inside the quotes). If they ask for single spaces, use " ".join(wordlist).
Want to practice more? If you found this helpful, I have compiled a full list of 30 practice problems just like this one. Each question focuses on a different logic to help you become a pro at File Handling.
Click here to view the full 30-Question Practice Set
Exam Special Recommendation: If you are studying in 12th class and preparing for your board exam, then go for the “Computer Science with Python Sample Paper Book“. It contains 3 previous years’ papers and 7 practice papers solved strictly as per the CBSE pattern. Click here to purchase your copy!