Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string on a number of different characters

I'd like to split a string using one or more separator characters.

E.g. "a b.c", split on " " and "." would give the list ["a", "b", "c"].

At the moment, I can't see anything in the standard library to do this, and my own attempts are a bit clumsy. E.g.

def my_split(string, split_chars):
    if isinstance(string_L, basestring):
        string_L = [string_L]
    try:
        split_char = split_chars[0]
    except IndexError:
        return string_L

    res = []
    for s in string_L:
        res.extend(s.split(split_char))
    return my_split(res, split_chars[1:])

print my_split("a b.c", [' ', '.'])

Horrible! Any better suggestions?

like image 811
James Brady Avatar asked Dec 17 '08 02:12

James Brady


People also ask

Can a string be split on multiple characters?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.

How do I split a string by counting characters?

If you want to know how to split a string by character count then this is easily done by using the split() function. This function returns a list which can be iterated over by a for loop with the range(start, stop, step) function.

How do I split a string into multiple parts?

You can split a string by each character using an empty string('') as the splitter. In the example below, we split the same message using an empty string. The result of the split will be an array containing all the characters in the message string.

Can split () take multiple arguments?

split() method accepts two arguments. The first optional argument is separator , which specifies what kind of separator to use for splitting the string. If this argument is not provided, the default value is any whitespace, meaning the string will split whenever .


2 Answers

>>> import re
>>> re.split('[ .]', 'a b.c')
['a', 'b', 'c']
like image 108
Ignacio Vazquez-Abrams Avatar answered Nov 09 '22 07:11

Ignacio Vazquez-Abrams


This one replaces all of the separators with the first separator in the list, and then "splits" using that character.

def split(string, divs):
    for d in divs[1:]:
        string = string.replace(d, divs[0])
    return string.split(divs[0])

output:

>>> split("a b.c", " .")
['a', 'b', 'c']

>>> split("a b.c", ".")
['a b', 'c']

I do like that 're' solution though.

like image 32
nakedfanatic Avatar answered Nov 09 '22 07:11

nakedfanatic