利用find,先判断开头,存不存在分隔符;再从b=e,利用while循环,进行判断
def my_split(str,sep):
e = str.find(sep)
split_list = []
if e == 0:
split_list.append('')
elif e == -1:
split_list.append(str[:])
else:
split_list.append(str[:e])
while e != -1:
b = str.find(sep,e)
e = str.find(sep,b+len(sep))
if b+len(sep) == e:
split_list.append('')
elif e == -1:
split_list.append(str[b + len(sep):])
else:
split_list.append(str[b + len(sep):e])
return split_list
s = 'w.......we..dasd..sd.as...ds.....gasd......ffg....hdf..d...h.....dasdasd...da......'
print('split: ',s.split('...'))
print('my_split',my_split(s,'...'))
结果:
split: ['', '', 'w', '', '.we..dasd..sd.as', 'ds', '..gasd', '', 'ffg', '.hdf..d', 'h', '..dasdasd', 'da', '', '']
my_split ['', '', 'w', '', '.we..dasd..sd.as', 'ds', '..gasd', '', 'ffg', '.hdf..d', 'h', '..dasdasd', 'da', '', '']