Python TypeError:拆分时应为字符缓冲区对象#

Python TypeError:拆分时应为字符缓冲区对象#,python,Python,这是我的密码: def split_string(source,splitlist): sl = list(splitlist) new = source.split(sl) return new 当我运行它时: print split_string("This is a test-of the,string separation-code!"," ,!-") 我有以下错误: new = source.split(sl) TypeError: expected a c

这是我的密码:

def split_string(source,splitlist):
    sl = list(splitlist)
    new = source.split(sl)
    return new
当我运行它时:

print split_string("This is a test-of the,string separation-code!"," ,!-")
我有以下错误:

new = source.split(sl)
TypeError: expected a character buffer object
我怎样才能解决这个问题

注意:首先,我想从
splitlist
创建一个列表,而不是将
source
与我的
sl
列表中的每个元素进行拆分


谢谢。

str.split的参数必须是字符串,而不是可能的分隔符列表。

我猜您正在寻找类似

import re
def multisplit(s, delims):
    delims = '|'.join(re.escape(x) for x in delims)
    return re.split(delims, s)

print multisplit('a.b-c!d', '.-!') # ['a', 'b', 'c', 'd']

str.split
不接受分隔符列表,尽管我希望它接受,就像
endswith

一样,没有其他库,您可以执行以下操作:

def split_string(source,splitlist):
    ss = list(source)
    sl = list(splitlist)
    new = ''.join([o if not o in sl else ' ' for o in ss]).split()
    return new
可能重复的