python提取字符串中日期

来源:互联网 发布:linux查看mysql实例名 编辑:程序博客网 时间:2024/06/04 17:41
import re#删除字符串中的中文字符def subChar(str):    match=re.compile(u'[\u4e00-\u9fa5]')    return  match.sub('',str)#提取日期def extractDate(str):    if not str:        return None    raw=subChar(str)    if not raw:        return None    #提取前10位字符    rawdate=raw[:10]    datelist=re.findall("\d+",rawdate)    if not datelist:        return None    if datelist.__len__()==3:        if (float(datelist[0])>2099 or float(datelist[0])<1900) or float(datelist[1])>12 or float(datelist[2])>31:            return None        else:            return '-'.join(datelist)    if datelist.__len__()==2:        if (float(datelist[0])>2099 or float(datelist[0])<1900) or float(datelist[1])>12:            return None        else:            datelist.append('01')            return '-'.join(datelist)    if datelist.__len__()==1:        if float(datelist[0])>20991231 or float(datelist[0])<19000101:            return None        else:            return datelist[0]    return None


本文章为转载,原文出处:http://blog.csdn.net/weisongming/article/details/78497909

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