By Juan

2018-11-08 23:24:55 8 Comments

I will be given a string, and I need to split it every time that it has an "|", "/", "." or "_"

How can I do this fast? I know how to use the command split, but is there any way to give more than 1 split condition to it? For example, if the input given was

Hello test|multiple|36.strings/just36/testing

I want the output to give:

"['Hello test', 'multiple', '36', 'strings', 'just36', 'testing']"


@dawg 2018-11-08 23:27:40

Use a regex and the regex module:

>>> import re
>>> s='You/can_split|multiple'
>>> re.split(r'[/_|.]', s)
['You', 'can', 'split', 'multiple']

In this case, [/_|.] will split on any of those characters.

Or, you can use a list comprehension to insert a single (perhaps multiple character) delimiter and then split on that:

>>> ''.join(['-><-' if c in '/_|.' else c for c in s]).split('-><-')
['You', 'can', 'split', 'multiple']

With the added example:

>>> s2="Hello test|multiple|36.strings/just36/testing"

Method 1:

>>> re.split(r'[/_|.]', s2)
['Hello test', 'multiple', '36', 'strings', 'just36', 'testing']

Method 2:

>>> ''.join(['-><-' if c in '/_|.' else c for c in s2]).split('-><-')
['Hello test', 'multiple', '36', 'strings', 'just36', 'testing']

@dawg 2018-11-08 23:38:57

Please add an example.

@dawg 2018-11-08 23:43:50

Is that the input or desired output? Please add example input and desired output to your question.

@Juan 2018-11-08 23:46:45

Added an example in the original post

@Juan 2018-11-08 23:50:08

Thank you! that worked. I was using jupyter notebook and the first command was giving me a weird output.

@Daniel Mesejo 2018-11-08 23:31:41

Use groupby:

from itertools import groupby

s = 'You/can_split|multiple'

separators = set('/_|.')
result = [''.join(group) for k, group in groupby(s, key=lambda x: x not in separators) if k]


['You', 'can', 'split', 'multiple']

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Meaning of @classmethod and @staticmethod for beginner?

11 Answered Questions

[SOLVED] Is there a way to substring a string?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2227456 View
  • 1688 Score
  • 11 Answer
  • Tags:   python string

57 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

19 Answered Questions

[SOLVED] Converting string into datetime

  • 2009-01-21 18:00:29
  • Oli
  • 2017253 View
  • 1590 Score
  • 19 Answer
  • Tags:   python datetime

15 Answered Questions

[SOLVED] How to delete a character from a string using Python

  • 2010-08-24 18:06:31
  • Lazer
  • 1114078 View
  • 341 Score
  • 15 Answer
  • Tags:   python string

14 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

14 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

16 Answered Questions

[SOLVED] Convert bytes to a string?

10 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 510990 View
  • 1273 Score
  • 10 Answer
  • Tags:   python

13 Answered Questions

[SOLVED] How can I tell if a string repeats itself in Python?

Sponsored Content