zoukankan      html  css  js  c++  java
  • 如何拆分含有多种分隔符的字符串

    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']
    
    """
    
  • 相关阅读:
    PS软件之,快速的修改图片你的尺寸
    想的太多,做的太少
    Java-Junit 的Hello world
    Java-hibernate的映射文件
    工作思路
    Spring入门Hello World
    PHP 进制问题
    Java-hibernate的Hello World
    PHP获得header头进行分析
    动软模板使用教程
  • 原文地址:https://www.cnblogs.com/xuehaozhe/p/ru-he-chai-fen-han-you-duo-zhong-fen-ge-fu-de-zi-f.html
Copyright © 2011-2022 走看看