如果只有1個分離器,你可以使用列表理解:
text = 'foo,bar,baz,qux'
sep = ','
附加/前面加上分隔符:
result = [x+sep for x in text.split(sep)]
#['foo,', 'bar,', 'baz,', 'qux,']
# to get rid of trailing
result[-1] = result[-1].strip(sep)
#['foo,', 'bar,', 'baz,', 'qux']
result = [sep+x for x in text.split(sep)]
#[',foo', ',bar', ',baz', ',qux']
# to get rid of trailing
result[0] = result[0].strip(sep)
#['foo', ',bar', ',baz', ',qux']
分離器,因爲它是自己的元素:
result = [u for x in text.split(sep) for u in (x, sep)]
#['foo', ',', 'bar', ',', 'baz', ',', 'qux', ',']
results = result[:-1] # to get rid of trailing
哪些呢'\ W'代表?我在谷歌上失敗了。 – Ooker 2015-08-29 19:26:39
一個_non-word_字符[詳見這裏](https://docs.python.org/2/library/re.html#regular-expression-syntax) – Russell 2015-12-02 21:27:03