TCL Python中的多字符拆分



我正在使用这个tcl proc:拆分一个文本文件

proc mcsplit "str splitStr {mc {x00}}" {
return [split [string map [list $splitStr $mc] $str] $mc]  }
# mcsplit --
#   Splits a string based using another string
# Arguments:
#   str       string to split into pieces
#   splitStr  substring
#   mc        magic character that must not exist in the orignal string.
#             Defaults to the NULL character.  Must be a single character.
# Results:
#   Returns a list of strings

split命令根据splitString中的每个字符拆分字符串。该版本将splitString作为组合字符串处理,但我的目标是使用python做同样的事情。这里有人以前做过同样的事情吗?

从您的问题中还不太清楚pythonsplit行为是否是您所需要的。如果您需要在每次出现多个字符串时进行拆分,Python的常规split将完成这项工作:

>>> 'this is a test'.split('es')
['this is a t', 't']

但是,如果要在出现多个单个字符时进行拆分,则需要使用re.split:

>>> import re
>>> re.split(r'[es]', 'this is a test')
['thi', ' i', ' a t', '', 't']
>>>

最新更新