环境:赢7; Python 2.76
我希望根据给定的separator
字符串如下:
“C-603WallWizard45256CCCylinders:2HorizontalOpposedBore:1-1/42006Stroke:1-1/8Length: SingleVerticalBore:1-111Height:6Width:K-720Cooling:AirWeight:6LBS1.5H.P.@54500RPMC-60150ccGas2007EngineCylinder:4VerticalInline2008Bore:1Stroke:1Cycle:42007Weight:6-1/2LBSLength:10Width: :AirLength16Cooling:AirLength:5Width:4L-233Height:6Weight: 4TheBlackKnightc-609SteamEngineBore:11/16Stroke:11/162008Length:3Width:3Height:4TheChallengerC-600Bore:1Stroke:1P-305Weight:18LBSLength:12Width:7Height:8C-606Wall15ccGasEngineJ-142Cylinder:SingleVerticalBore:1Stroke:1-1/8Cooling:1Stroke:1-1/4HP:: /4Stroke:1-7/:6Width:6Height:92006Weight:4LBS1.75H.P.@65200RPM”
separator
是:[‘2006’, ‘2007’, ‘2008’, ‘2009’]
如何实现?
之前我已经检查了帖子Split Strings with Multiple Delimiters?但是我认为不需要和帖子有关如何将字符串拆分成单词一样。我的上面是根据列表中元素的分割。
答案 0 :(得分:0)
如果要按任何这些分隔符的任何实例进行拆分,您可能需要正则表达式:
r = re.compile(r'|'.join(separators))
r.split(s)
请注意,这通常不是构建regexp的安全方法;它只能在这里工作,因为我知道你的分隔符都不包含任何特殊字符。如果您不确定,请使用re.escape
:
r = re.compile(r'|'.join(map(re.escape, separators)))
如果您希望按照这些分隔符完全按顺序拆分一次,那么您可能希望使用str.split
或str.partition
:
bits = []
for separator in separators:
first, _, s = s.partition(separator)
bits.append(first)
bits.append(s)