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



兩種方法:

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

print re.split('[,;\|\t "]+', str)


判斷字符串str是否以a開頭或結尾

import os, stat

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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章