如何通过分隔符将句子分成两部分

时间:2014-12-19 01:46:59

标签: python

我的例句是' _'分离器:

s = "This_is_some_sentence_ID"
splited = s.split('_')
print(splited)

这就像['这',','某些','句子',' ID&# 39]

但是我怎样才能得到两个部分,比如[' This_is_some_sentence',' ID']?

1 个答案:

答案 0 :(得分:4)

您可以使用rsplit

In [3]: s = "This_is_some_sentence_ID"

In [4]: splited = s.rsplit('_', 1)

In [5]: print(splited)
['This_is_some_sentence', 'ID']