I have a formatted string from a log file, which looks like:
>>> a="test result"
That is, the test and the result are split by some spaces - it was probably created using formatted string which gave test
some constant spacing.
Simple splitting won't do the trick:
>>> a.split[" "]
['test', '', '', '', ... '', '', '', '', '', '', '', '', '', '', '', 'result']
split[DELIMITER, COUNT]
cleared some unnecessary values:
>>> a.split[" ",1]
['test', ' result']
This helped - but of course, I really need:
['test', 'result']
I can use split[]
followed by map
+ strip[]
, but I wondered if there is a
more Pythonic way to do it.
Thanks,
Adam
UPDATE: Such a simple solution! Thank you all.
Contents
- Introduction
- Method 1: Using Split String
- Method 2: Using Regular Expression
- Summary
There are many ways to replace multiple white spaces with a single space like using a string split or regular expression module. We will see each one of them with examples.
Method 1: Using Split String
- Read the input text file in read mode and output file in write mode.
- For each line read from then input file
- Split the given string using split[] method. By default split[] method splits with space as delimiter. When there are multiple spaces, those splits are ignored and hence we will get individual words.
- Join the splits using single space ‘ ‘.
- Write to the output file.
- Close input and output files.
Example
In the following example, we will replace multiple spaces with 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[]
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 expression to find continuous multiple white spaces and replace them with a single space.
- Import re module. re for regular expression.
- Read the input Text file in read mode and output Text file in write mode.
- For each line read from text file, use re.sub[] method. sub[] substitutes or replaces a string with another string in the text provided.
- Close input and output files.
Example
In the following example, we will replace all multiple white spaces with single white space using 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[]
Summary
In this tutorial of Python Examples, we learned how to replace multiple white space characters with a single space, using different approaches and example programs for each of them.
Contents
- Introduction
- Example 1: Split String by Space
- Example 2: Split String by One or More Adjacent Spaces
- Example 3: Split String by Any White Space Character
- Summary
You can split a string with space as delimiter in Python using String.split[] method.
In this tutorial, we will learn how to split a string by a space character, and whitespace characters in general, in Python using String.split[] and re.split[] methods.
Refer Python Split String to know the syntax and basic usage of String.split[] method.
Example 1: Split String by Space
In this example, we will take a string which contains words/items/chunks separated by space character. We shall then split the string by space using String.split[] method. split[] method returns list of chunks.
Python Program
str = '63 41 92 81 69 70'
#split string by single space
chunks = str.split[' ']
print[chunks]
Run
Output
['63', '41', '92', '81', '69', '70']
Example 2: Split String by One or More Adjacent Spaces
In this example, we will take a string with chunks separated by one or more single space characters. Then we shall split the string using re.split[] function. re.split[] returns chunks in a list.
We shall use re
python package in the following program. re.split[regular_expression, string]
returns list of chunks split from string
based on the regular_expression
.
Python Program
import re
str = '63 41 92 81 69 70'
#split string by single space
chunks = re.split[' +', str]
print[chunks]
Run
Regular Expression +
represents one or more immediately occuring spaces. So, one or more single space characters is considered as a delimiter.
Output
['63', '41', '92', '81', '69', '70']
One ore more adjacent spaces are considered as a single delimiter because of the regular expression.
Example 3: Split String by Any White Space Character
In this example, we shall split the string into chunks with any white space character as delimiter.
Following are the list of white space characters from ASCII Table.
ASCII Hex Code | Description |
09 | horizontal tab |
0A | New line feed |
0B | Vertical Tab |
0D | Carriage Return/ Form Feed |
20 | Space |
By default, String.split[], with no argument passed, splits the string into chunks with all the white space characters as delimiters.
Python Program
import re
str = '63 41\t92\n81\r69 70'
#split string by single space
chunks = str.split[]
print[chunks]
Run
Output
['63', '41', '92', '81', '69', '70']
Summary
In this tutorial of Python Examples, we learned how to split a string by space using String.split[] and re.split[] methods. Also, we learned how to split a string by considering all whitespace characters as delimiter.
Related Tutorials
- Python Split String into Specific Length Chunks
- Python Split String by Comma
- How to Split String by Underscore in Python?
- Python Split String into List of Characters
- Python Split String by New Line