在Python 3中删除部分字符串

时间:2016-05-15 03:17:20

标签: python python-3.x

因此,我一直在查看重新记录文档,搜索网页,并尝试了几个小时,现在仍然无法找到删除字符串某个部分的方法。

所以,字符串看起来像这样,

  Linda Lost(Secret Icecone Incorporated)在Osmon(The Forge)失去了他们的Kronos。 Liberty Prime(退出战略)在仲裁员中飞行的最后一击。总价值:1,865,802,910.96 ISK

基本上,当从网站的元数据中提取字符串时,字符串中的所有名称和内容都会发生变化。唯一可以确定的事情就是' Final Blow by',' Total Value:'和' ISK'会是一样的。

所以,我一直想弄清楚要做的是删除整个'总价值:1,865,802,910.96 ISK'在它之前的部分和回归。

非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

这样可行。

t = "Linda Lost (Secret Icecone Incorporated) lost their Kronos in Osmon (The Forge). Final Blow by Liberty Prime (Exit-Strategy) flying in a Arbitrator. Total Value: 1,865,802,910.96 ISK"

其中= t.rfind(“总计”)

打印(T [:其中])

答案 1 :(得分:2)

您可以按如下方式使用正则表达式

pat = re.compile(r'(.*?)Total Value: [\d,.]* ISK')
m = pat.match(s)
m.group(1)
'Linda Lost (Secret Icecone Incorporated) lost their Kronos in Osmon (The Forge). Final Blow by Liberty Prime (Exit-Strategy) flying in a Arbitrator. '

或者您可以执行以下操作

s.rsplit('.',2)[0]
'Linda Lost (Secret Icecone Incorporated) lost their Kronos in Osmon (The Forge). Final Blow by Liberty Prime (Exit-Strategy) flying in a Arbitrator'

答案 2 :(得分:0)

这有效

t = "Linda Lost (Secret Icecone Incorporated) lost their Kronos in Osmon (The Forge). Final Blow by Liberty Prime (Exit-Strategy) flying in a Arbitrator. Total Value: 1,865,802,910.96 ISK"

t.split('Total Value:')[0]