-1
我想分割使用多个分隔符的字符串。我需要将分隔符保留为单词。 我使用的分隔符是:所有标点符号和空格。Python拆分字符串并将分隔符保存为一个词
例如,字符串:
Je suis, FOU et toi ?!
应该产生:
'Je'
'suis'
','
'FOU'
'et'
'toi'
'?'
'!'
我写道:
class Parser :
def __init__(self) :
"""Empty constructor"""
def read(self, file_name) :
from string import punctuation
with open(file_name, 'r') as file :
for line in file :
for word in line.split() :
r = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
print(r.split(word))
但我得到的结果是:
['Je']
['suis', '']
['FOU']
['et']
['toi']
['', '']
拆分似乎是正确的,但结果列表中不包含分隔符:(