-3

Environment: Win 7; Python 2.76

I want to split a long string into pieces, according to given separator

The string looks like:

“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”

The separator are: [‘2006’, ‘2007’, ‘2008’, ‘2009’]

How is it possible to achieve?

Update

I've checked the post Split Strings with Multiple Delimiters? before however I don't think the need is the same as the post is about how to split a string into words. Mine's above is a split according to elements in a list.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • This is not necessary a trivial question **if** the string is **very long**. – Sylvain Leroux Aug 27 '14 at 09:36
  • Is Google broken? https://docs.python.org/2/library/re.html#re.split – jonrsharpe Aug 27 '14 at 09:37
  • Please edit your question to clarify some things: how long is the string ? would you split on any occurrence of `2006`, `2007`, etc. Are the "newline" part of your string ? – Sylvain Leroux Aug 27 '14 at 09:38
  • 2
    Although the marked duplicate is the canonical answer, you might find the answers to [this other duplicate](http://stackoverflow.com/questions/4998629/python-split-string-with-multiple-delimiters) more immediately useful to you. – abarnert Aug 27 '14 at 09:43
  • 1
    Also, if your question is "I want these separators exactly once, in exactly this order", then the question is _not_ a duplicate, so please edit your question to make that clear so it can be reopened. – abarnert Aug 27 '14 at 09:44
  • thanks, Sylvain Leroux and abarnert. – Mark K Aug 28 '14 at 08:09
  • 1
    @abarnert It doesn't seem to be about the order since `2007` appears before and after `2008`. – Artjom B. Aug 28 '14 at 08:31
  • 1
    @MarkK: "I've checked the post Python strings split with multiple delimiters before": This is exactly why SO keeps dups around, and shows them in Linked on the canonical question. It may be not at all obvious to a novice that your question is the same as the original, but if you look at some of the linked questions there (like the one I linked above), it's a lot more obvious. For example, kLeos wanted to split on any `,` or `;`, just like you want to split on any `2006`, `2007`, `2008`, or `2009`. And now your question appears on the links too, which may help other future searchers. – abarnert Aug 29 '14 at 00:04

1 Answers1

0

If you want to split by any instance of any of those separators, you may want a regular expression:

r = re.compile(r'|'.join(separators))
r.split(s)

Note that this is not a safe way to build regexps in general; it only works here because I know that none of your separators contain any special characters. If you don't know that for sure, use re.escape:

r = re.compile(r'|'.join(map(re.escape, separators)))

If you want to split by those separators exactly once each, in exactly that order, you probably want to use either str.split or str.partition:

bits = []
for separator in separators:
    first, _, s = s.partition(separator)
    bits.append(first)
bits.append(s)
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • dear abarnert, thank you indeed. I am amazed always by what it can do. your help and guidance is very important in my learning. thank you again. – Mark K Aug 28 '14 at 08:03