例如:
String1='Hi what are you doing?'
应分为:
List1=['Hi','\s','what','\s','are','\s','you','\s','doing','\s','?']
答案 0 :(得分:2)
如果您只想拆分:
String1='Hi what are you doing ?'
print(String1.split())
输出:
['Hi', 'what', 'are', 'you', 'doing', '?']
如果您想要如示例所示:
print(String1.replace(" "," \s ").split())
输出:
['Hi', '\\s', 'what', '\\s', 'are', '\\s', 'you', '\\s', 'doing', '\\s', '?']
答案 1 :(得分:0)
import re
s = your string here \nhello" re.split('\s+', s)
通过re模块的另一种方法。
>>> import re
>>> s = "your string here \nhello \thi"
>>> re.findall(r'\S+', s) ['your', 'string', 'word', 'hello', 'hi']
这将匹配一个或多个非空格字符。
答案 2 :(得分:0)
试试这个:
s ='Hi what are you doing?'
import re
re.findall('[a-zA-Z]{1,}|[^a-zA-Z]{1,}', s)
输出:
['Hi', ' ', 'what', ' ', 'are', ' ', 'you', ' ', 'doing', '?']