有没有相当于str.split
在Python中也返回分隔符?
在处理一些标记后,我需要保留输出的空白布局。
Example:
>>> s="\tthis is an example"
>>> print s.split()
['this', 'is', 'an', 'example']
>>> print what_I_want(s)
['\t', 'this', ' ', 'is', ' ', 'an', ' ', 'example']
Thanks!
怎么样
import re
splitter = re.compile(r'(\s+|\S+)')
splitter.findall(s)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)