Python中是否有一个函数来分割字符串而不忽略结果列表中的空格?
例如:
s="This is the string I want to split".split()
给我
>>> s ['This', 'is', 'the', 'string', 'I', 'want', 'to', 'split']
我想要类似的东西
['This',' ','is',' ', 'the',' ','string', ' ', .....]
Greg Hewgill.. 41
>>> import re >>> re.split(r"(\s+)", "This is the string I want to split") ['This', ' ', 'is', ' ', 'the', ' ', 'string', ' ', 'I', ' ', 'want', ' ', 'to', ' ', 'split']
在re.split()中使用捕获括号会导致函数返回分隔符.
>>> import re >>> re.split(r"(\s+)", "This is the string I want to split") ['This', ' ', 'is', ' ', 'the', ' ', 'string', ' ', 'I', ' ', 'want', ' ', 'to', ' ', 'split']
在re.split()中使用捕获括号会导致函数返回分隔符.