Python - Replace Multiple Spaces with Single Space in a Text File


Python - Replace Multiple Spaces with Single Space in a Text File

There are several ways to replace multiple white spaces with a single space, such as using the string split method or the regular expression module. In this tutorial, we will cover each approach with examples.


Method 1: Using Split String

  1. Read the input text file in read mode and the output file in write mode.
  2. For each line read from the input file:
    1. Split the string using the split() method. By default, the split() method splits based on spaces. When there are multiple spaces, they are ignored, and individual words are returned.
    2. Join the resulting list of words using a single space ' '.
    3. Write the modified line to the output file.
  3. Close both the input and output files.

Example

In the following example, we will replace multiple spaces with a single space.

Python Program

fin = open("data.txt", "rt")
fout = open("out.txt", "wt")

for line in fin:
	fout.write(' '.join(line.split()))
	
fin.close()
fout.close()

Explanation

  1. Open the file data.txt in read mode and out.txt in write mode.
  2. For each line in the input file, use split() to break the line into words, ignoring any extra spaces.
  3. Join the words with a single space using ' '.join().
  4. Write the result to the output file and close both files after processing.

Input file:

Welcome to      www.pythonexamples.org. Here, you will     find python programs              for all        general use cases.

Output file:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

All multiple white spaces in the text file are replaced with a single white space.


Method 2: Using Regular Expression

You can also use regular expressions to find continuous multiple white spaces and replace them with a single space.

  1. Import the re module, which provides support for regular expressions.
  2. Read the input text file in read mode and the output text file in write mode.
  3. For each line in the input file, use the re.sub() method to substitute multiple spaces with a single space. The re.sub() method finds the pattern \s+ (which matches one or more spaces) and replaces it with a single space.
  4. Close the input and output files.

Example

In this example, we will replace all multiple white spaces with a single white space using the re module.

Python Program

import re

fin = open("data.txt", "rt")
fout = open("out.txt", "wt")

for line in fin:
	fout.write(re.sub('\s+',' ',line))
	
fin.close()
fout.close()

Explanation

  1. Import the re module to use regular expressions.
  2. Open the input and output files in read and write modes, respectively.
  3. Use re.sub('\s+', ' ', line) to replace all instances of multiple spaces (represented by \s+) with a single space.
  4. Write the modified line to the output file and close both files.

Example 3: Replace Multiple Spaces and Remove Leading/Trailing Spaces

If you want to remove leading and trailing spaces from each line in addition to replacing multiple spaces, you can modify the methods slightly.

Python Program

import re

fin = open("data.txt", "rt")
fout = open("out.txt", "wt")

for line in fin:
	line = re.sub('\s+', ' ', line).strip()
	fout.write(line)

fin.close()
fout.close()

Explanation

  1. After replacing multiple spaces with a single space, use the strip() method to remove any leading or trailing spaces.
  2. Write the cleaned line to the output file and close both files.

Input file:

    Welcome to      www.pythonexamples.org. Here, you will     find python programs              for all        general use cases.   

Output file:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

Method 4: Replace Multiple Spaces with Single Space and Handle Empty Lines

If your file contains empty lines that you want to skip or clean, you can add a simple check before processing each line.

Python Program

import re

fin = open("data.txt", "rt")
fout = open("out.txt", "wt")

for line in fin:
	if line.strip():
		fout.write(re.sub('\s+', ' ', line))

fin.close()
fout.close()

Explanation

  1. Before processing each line, check if the line is not empty using line.strip().
  2. If the line contains any text, replace multiple spaces with a single space using re.sub().
  3. Write the cleaned line to the output file and close both files.

Summary

In this tutorial of Python Examples, we learned how to replace multiple white space characters with a single space using different methods. We demonstrated replacing spaces with the split() method, the re.sub() method, and other variations for handling leading/trailing spaces or empty lines.


Python Libraries