python拆分含有多种分隔符的字符串

来源:互联网 发布:知微数据 编辑:程序博客网 时间:2024/05/17 23:07



两种方法:

'''1.使用split方法,每次处理一种分隔符2.正则表达式'''
str = 'dimples 1994 0.0 5454 \\2017/9.27 "haha" '
方法一
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(str, ';,\|\t"')
方法二
import reprint re.split('[,;\|\t "]+', str)


判断字符串str是否以a开头或结尾

import os, stats = 'g.gh'print s.endswith(('.gh', ',py'))
print [name for name in s if name.endswith(('.sh', ',py'))]





阅读全文
0 0
原创粉丝点击