Using Python to split long string, by given ‘separators’ [duplicate]

2024/11/24 14:32:13

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.

Answer

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)
https://en.xdnf.cn/q/120522.html

Related Q&A

How do I return the number of unique digits in a positive integer

Example: unique_dig(123456) All unique 6Im trying to write code to have a function return how many unique numbers there are in a positive integer.count = 0for i in unique_digits:if count.has_key(i):cou…

Python check json file with variables

I have a json file which has 18 substrings like this: https://i.sstatic.net/aVWuw.png https://i.sstatic.net/RLlRX.pngBut I have more json files who have different number of these substrings. So I did t…

The Sum of Consecutive Numbers in Python

What I have to do is get the user input and add consecutive numbers starting with one using a loop until the sum equals or exceeds the input. Its an exercise, so Im trying to do this without using the …

how to write to a text file using python ?

I am trying to output a full for iteration. The output should be in a text file. How should I code for that ? The output should look like :Iteration 1 values --------> val1 < tab > val2 < …

Python: Sorting dictionary by key

I am trying to sort a dictionary by key.If I do the following, then the dictionary is sorted like this1, 20 10, 5 11, 3 2, 30 20, 2Instead, I wanted to sort it like the following:1, 20 2, 30 10, 5 11, …

Please see my problem, believe me it is easy to solve

i tried to implement async and await inside spawn child process. But it didnt worked. Please see this Expected output************* http://www.stevecostellolaw.com/************* http://www.stevecostello…

How to make discord bot ping users using discord.py [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

how to edit hostname file using fabric

I have change my hosts file,so how to change hostname.my system is ubuntu. eg my hosts file:192.168.0.100 host1.mydomain.com 192.168.0.101 host2.mydomain.comI wanna the hostname file under /etc/hostnam…

functions and indentation in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 3 days ago.Im taking a tutorial in udemy t…

How do I define a method to store a collection (e.g., dictionary)?

Im a beginner working on a library management system and theres something I cant get my head around. So I have a Books class that creates new book records. class Books:def __init__(self, title=None, au…