当前位置:  开发笔记 > 编程语言 > 正文

Python中是否有一个函数来分割字符串而不忽略空格?

如何解决《Python中是否有一个函数来分割字符串而不忽略空格?》经验,为你挑选了1个好方法。

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()中使用捕获括号会导致函数返回分隔符.



1> Greg Hewgill..:
>>> import re
>>> re.split(r"(\s+)", "This is the string I want to split")
['This', ' ', 'is', ' ', 'the', ' ', 'string', ' ', 'I', ' ', 'want', ' ', 'to', ' ', 'split']

在re.split()中使用捕获括号会导致函数返回分隔符.

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有