如何拆分含有多种分隔符的字符串

In [6]: s
Out[6]: 'www       1227  0.0  0.0  15572  2096 pts/2    R+   10:29   0:00 ps aux'

In [8]: s.split?
Docstring:
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
Type:      builtin_function_or_method

In [10]: s.split()
Out[10]:
['www',
 '1227',
 '0.0',
 '0.0',
 '15572',
 '2096',
 'pts/2',
 'R+',
 '10:29',
 '0:00',
 'ps',
 'aux']

如何拆分含有多种分隔符的字符串

方法1

如何拆分含有多种分隔符的字符串

修改成函数:
如何拆分含有多种分隔符的字符串

如果出现空格

如何拆分含有多种分隔符的字符串

方法2

如何拆分含有多种分隔符的字符串

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2017-03-30 11:16:06
# @Author  : Xue Haozhe (xuehaozhe0526@163.com)
# @Link    : http://haozhe.site
# @Version : $Id$

import re

s = 'www@muke-62-4595;871-bpjij:/data/webroot, ,$ www@muke-62-4595871-bpjij:/data/webroot$'
ss = 'www@muke624595;871bpjij:/data/webroot, ,$ www@muke624595871bpjij:/data/webroot$'
def mySplit(s, ds):
    res = [s]

    for d in ds:
        t = []
        map(lambda x: t.extend(x.split(d)), res)
        res = t
    return [x for x in res if x]


print mySplit(s,'@-;:/$')


print re.split(r'[@:/$]+',ss)

"""
['www', 'muke', '62', '4595', '871', 'bpjij', 'data', 'webroot, ,', ' www', 'muke', '62', '4595871', 'bpjij', 'data', 'webroot']

"""

相关文章: