将句子从句号、逗号和“& bu”分解成各部分

2024-09-30 18:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

sentence = "Very disorganized and hard professor. Does not come to classes on time, she grades tough, does not help on anything. She says come for help but when you go to her office hour, she is not there to help."

我想把这个句子分成句号、逗号和&;的几个部分;但是。 输出应该是

Very disorganized 
and hard professor. 
Does not come to classes on time, 
she grades tough, 
does not help on anything. 
She says come for help 
but when you go to her office hour, 
she is not there to help. 

现在我用的是

sample = re.split(r' *[\.\?!][\'"\)\]]* *', sentence)

print (sample)

这只会打断句号。 输出

['Very disorganized and hard professor', 'Does not come to classes on time, she grades tough, does not help on anything', 'She says come for help but when you go to her office hour, she is not there to help']

你知道怎么做吗


Tags: andtotimeonnothelpclassesvery
3条回答

或者一个循环

for x in ['.', ',', 'and', 'but']:
   sentence=sentence.replace(x, x+'\n')

在每个分隔符后添加\n

输出:

Very disorganized and
 hard professor.
 Does not come to classes on time,
 she grades tough,
 does not help on anything.
 She says come for help but
 when you go to her office hour,
 she is not there to help.

可以使用^{}在遇到停止字的地方添加换行符

正则表达式很简单:(and|\.|but|,),它与你的stopwords匹配。然后将该组替换为自身,再加上一个换行符

>>> import re   
>>> sentence = "Very disorganized and hard professor. Does not come to classes on time, she grades tough, does not help on anything. She says come for help but when you go to her office hour, she is not there to help."  
>>> sample = re.sub(r'(and|\.|but|,)', r'\1\n', sentence)
>>> sample
Very disorganized and
 hard professor.
 Does not come to classes on time,
 she grades tough,
 does not help on anything.
 She says come for help but
 when you go to her office hour,
 she is not there to help.

如果要将其列在列表中:

>>> re.sub(r'(and|\.|but|,)', r'\1\n', sentence).split('\n')
['Very disorganized and', ' hard professor.', ' Does not come to classes on time,', ' she grades tough,', ' does not help on anything.', ' She says come for help but', ' when you go to her office hour,', ' she is not there to help.', '']

如果要删除以下每行前面的空白,可以使用以下方法:

sample = re.sub(r'(and|\.|but|,)(?:\s)', r'\1\n', sentence)

试试这个

Result=re.split(r'[.,&]', sentence)

相关问题 更多 >