How do you split one space or more in python?

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

  1. Read the input text file in read mode and output file in write mode.
  2. For each line read from then input file
    1. 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.
    2. Join the splits using single space ‘ ‘.
    3. Write to the output file.
  3. 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.

  1. Import re module. re for regular expression.
  2. Read the input Text file in read mode and output Text file in write mode.
  3. For each line read from text file, use re.sub[] method. sub[] substitutes or replaces a string with another string in the text provided.
  4. 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

How do I make one space in Python?

Replace multiple spaces with a single space in Python #.
Use the str. split[] method to split the string on each whitespace character..
Use the str. join[] method to join the list of strings with a space..
The words in the new string will be separated by a single space..

How do I split a string into multiple spaces?

To split a string by multiple spaces, call the split[] method, passing it a regular expression, e.g. str. trim[]. split[/\s+/] . The regular expression will split the string on one or more spaces and return an array containing the substrings.

Chủ Đề