Python Forum
splitting on 2 or more possible characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting on 2 or more possible characters
#4
If 'first replace then split' suits your needs it quite easy to implement on containers:

>>> a = 'bacon-eggs_spam*ham!foo&bar'
>>> breakpoints = ['_', '*', '!', '&']
>>> for char in a:
...     if char in breakpoints:
...         a = a.replace(char, '-')
...
>>> a.split('-')
['bacon', 'eggs', 'spam', 'ham', 'foo', 'bar']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: splitting on 2 or more possible characters - by perfringo - Sep-02-2018, 07:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove escape characters / Unicode characters from string DreamingInsanity 5 14,455 May-15-2020, 01:37 PM
Last Post: snippsat
  splitting or parsing control characters Skaperen 19 11,590 May-26-2017, 07:27 PM
Last Post: zivoni
  splitting a string by 2 characters Skaperen 8 9,185 Dec-27-2016, 06:14 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020