python split 多个空格分隔

python split 多个空格分割

问题描述

即字符串之间存在多个空格的时候,不按照一个空格来分隔,而是按照空白来分隔。
比如

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

预期输出为

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

但是像如上做法的话是按照一个空格来分隔,结果为:

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

正确做法

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

真实输出和预期一致,为:

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

使用re实现多个空格分隔

猜你喜欢

转载自blog.csdn.net/qq_32507417/article/details/107505719