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
#2
If there are only two symbols you want to split on then you can do replace and then split.

>>> a = 'spam_ham-bacon_eggs'
>>> a.replace('_', '-').split('-')
['spam', 'ham', 'bacon', 'eggs']
It will not give you any errors when symbols are not present so no need to catch error with try...except.
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, 05:22 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