python split multiple spaces separated

python split multiple spaces split

Problem Description

That is, when there are multiple spaces between the strings, they are not separated by one space, but separated by whitespace.
such as

ll = "a  b  c    d"
print(ll.split(" "))

The expected output is

['a', 'b', 'c', 'd']

But like the above method, it is separated by a space, and the result is:

['a', '', 'b', '', 'c', '', '', '', 'd']

Correct way

import re
ll = "a  b  c    d"
print("re", re.split(r"[ ]+", ll))

The actual output is consistent with expectations, as:

re ['a', 'b', 'c', 'd']

Use re to achieve multiple space separation

Guess you like

Origin blog.csdn.net/qq_32507417/article/details/107505719