【问题标题】:Remove parts of a sentence in python [closed]在python中删除句子的一部分[关闭]
【发布时间】:2020-04-20 10:25:17
【问题描述】:

我有一个句子如下

例如:

原始字符串可以是以下格式。

"[PartnerID:2012345][Failure] Caused by :This is the Failure Name"
"[Failure] caused by This is the failure name"
"Reliability: oscrash in This is the failure name"

我希望将字符串修剪为This is the Failure Name

谁能帮我在python中为此编写代码? 对不起,我之前漏掉了其他句子

【问题讨论】:

  • 到目前为止你尝试过什么?拆分字符串是一个非常基本的操作,您需要任何高级规则吗?您提取句尾的规则究竟是什么?最后 24 个字符? :Cause by :[Failure] Caused by : 之后的所有内容?是否存在您想要拆分的边缘情况,例如... Caused by: Invalid literal for int: 24.0 会导致 24.0Invalid literal for int: 24.0?
  • 我之前省略了其他句子。对此表示歉意。现在更正了。

标签: python python-3.x string


【解决方案1】:

让:

str1 = "[PartnerID:2012345][Failure] Caused by :This is the Failure Name"

为了得到你所需要的,我们分开了:

res = str1.split(":")[-1]

如果是这样的:

str1 = "[PartnerID:2012345][Failure] Caused by This is the Failure Name"

那么你可以:

res = str1.split("Caused by ")[-1]

【讨论】:

  • 好的,如果句子中有[Failure]这样的变体是由This is the failure name引起的,那该怎么办呢?
  • 我们需要知道你的句子中哪一部分是不变的。也许您可以随时在"[Failure]" 拆分?
  • @VinyasBhat 我也添加了。如果这有帮助。不要忘记点击我的答案旁边的勾:)
  • @JoshuaVarghese 来吧,失败名称不会总是以“T”开头...split("Caused by") 更加健壮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多