使用re,多个分割符分割字符串及应用(万能字符串时间转时间格式)
# 一个时间字符串
s = '2019-7-3 12:00:00'
'''介绍一个万能的时间字符串转时间的方法'''
from datetime import datetime
d = [int(item) for item in re.split('[- :]', s)]
res = datetime(*d)
res
datetime.datetime(2019, 7, 3, 12, 0)
2019-07-03 12:00:00
re.split()使用的时候注意’[- :]'不能以空格开头
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
['2019', '7', '3', '12', '00', '00']
re在字符串的处理上还有其他的比如格式化替换原则等,需要持续学习。