我需要在不删除Python中分隔符的情况下拆分字符串。
例如:
content = 'This 1 string is very big 2 i need to split it 3 into paragraph wise. 4 But this string 5 not a formated string.'
content = content.split('\s\d\s')
在此之后,我得到的是这样的:
This\n
string is very big\n
i need to split it\n
into paragraph wise.\n
But this string\n
not a formated string.
但我想这样:
This\n
1 string is very big\n
2 i need to split it\n
3 into paragraph wise.\n
4 But this string\n
5 not a formated string
您可以re.split
与前瞻性配合使用:
import re
re.split('\s(?=\d\s)',content)
导致:
['This', '1 string is very big', '2 i need to split it', '3 into paragraph wise.', '4 But this string', '5 not a formated string.']
这会在空格上分割-但仅是那些紧跟着一个数字然后是另一个空格的空格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句