IIUC,你需要用每个分隔符来分割字符串,所以你可以这样做:
symbols = "@, &, %, $".split(', ')
print(symbols) # this is a list
text = "The first @ The second & and here you have % and finally $"
# make a copy of text
replaced = text[:]
# unify delimiters
for symbol in symbols:
replaced = replaced.replace(symbol, '@')
print(replaced) # now the string only have @ in the place of other symbols
for chunk in replaced.split('@'):
if chunk: # avoid printing empty strings
print(chunk)
输出
['@', '&', '%', '$'] # print(symbols)
The first @ The second @ and here you have @ and finally @ # print(replaced)
The first
The second
and here you have
and finally
第一步:
symbols = "@, &, %, $".split(', ')
print(symbols) # this is a list
将您的字符串转换为列表。第二步使用replace 替换所有符号,因为str.split 仅适用于单个字符串:
# unify delimiters
for symbol in symbols:
replaced = replaced.replace(symbol, '@')
第三步也是最后一步是按所选符号(即@)分割字符串:
for chunk in replaced.split('@'):
if chunk: # avoid printing empty strings
print(chunk)