python集合的定界符_如何在python中使用带有split()的两个或多个定界符

我有一些文本,其中定界符可以是列表中的任何内容[;,。?]

txt1 = "Kids of today have started selling drugs or taken drugs at this age, then we are finished as parent,what generation are we going to have when our generation is no more,am sick to my stomach, it means we do not have tomorrow leaders or future leader, drugs at this stage woowowow parent and Guidance's fasten your belt if not we will wake up someday to see what we never thought could happen"

txt2 = "There was a clear warning sign, and this person chose to take a risk regardless. It was quite a stupid decision to climb the fence, but even this is probably a common activity that generally never results in death. More of a freak accident than a definite way for someone to die. At the very most, the only changes that should be made by the airport / authorities would be to the fence design, making it more difficult for people to climb up. Barricading the area off completely and banning people from the area would be comparable to fencing off a scenic mountain path that hundreds of people like to climb and enjoy safely, but which does produce the occasional fatality when people slip. Just because this area carries a (clearly communicated) risk shouldn't be a reason for the authorities to step in and make adjustments. People take risks and are responsible for their own safety in areas like this. One fatality is a tiny drop in the bucket compared to the hundreds of people doing this each month without incident."

如何根据分隔符的存在将多行句子分成独立的句子。例如,在txt1中,定界符应为','(逗号),而在txt2中,定界符应为'。'(点)。

我已经为此使用了re.split(),但是没有得到期望的结果。我用了:

print(re.split(';|,|.|?',txt1))

解决方案

您必须在和前面添加转义字符\.?

print(re.split(';|,|\.|\?',txt1))

为了避免出现空白字符/空strig,请进行列表理解

[x for x in re.split(';|,|\.|\?',txt1) if x]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值