2.1.如何拆分含有多种分隔符的字符串
#2.1.如何拆分含有多种分隔符的字符串
s = 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz'
#第一种方法
def my__split(s, seps):
res = [s]
for sep in seps:
t = []
list(map(lambda ss: t.extend(ss.split(sep)), res))
res = t
return res
s1 = my__split(s, ',;|\t')
print(s1) #['ab', 'cd', 'efg', 'hi', 'jkl', 'mn', 'opq', 'rst', 'uvw', 'xyz']
#第二种方式:使用re.split (推荐)
import re
s2 = re.split('[,;|\t]+', s)
print(s2) #['ab', 'cd', 'efg', 'hi', 'jkl', 'mn', 'opq', 'rst', 'uvw', 'xyz']
2.2.如何调整字符串中文本的格式
#2.2.如何调整字符串中文本的格式
import re
#调整时间显示的格式
s = "2019-08-15 23:23:12"
s1 = re.sub(r'(\d{4})-(\d{2})-(\d{2})',r'\2/\3/\1',s)
print(s1) # 08/15/2019 23:23:12