Juan
Juan

Reputation: 133

Splitting a string after certain characters?

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']"

Upvotes: 2

Views: 1318

Answers (2)

dawg
dawg

Reputation: 103714

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']

Upvotes: 3

Dani Mesejo
Dani Mesejo

Reputation: 61900

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]
print(result)

Output

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

Upvotes: 0

Related Questions