两种方法:
'''
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'))]
本文介绍了两种字符串分割的方法:一是使用Python的split方法逐步处理多种分隔符;二是利用正则表达式进行复杂模式匹配。此外还展示了如何判断字符串是否以特定字符开头或结尾。
1万+

被折叠的 条评论
为什么被折叠?



