实现统一时间格式的函数

来源:互联网 发布:中国isis知乎 编辑:程序博客网 时间:2024/05/26 07:28
#-*- coding:utf-8 -*-import timedef uninformTM2(arttm, tmrul):    if not arttm: return None    arttm=arttm.replace(' ',' ').replace('-','-').replace(':',':').replace('  ',' ').strip()    tmrultmp=tmrul    while len(tmrultmp)>0 and tmrultmp[0]!='%':        if tmrultmp[0]==' ':arttm=arttm.lstrip()        arttm=arttm.split(tmrultmp[0],1)[1].strip()        tmrultmp=tmrultmp[1:]    while len(tmrultmp)>1 and tmrultmp[-2]!='%':    if tmrultmp[-1]==' ':            arttm=arttm.rstrip()        arttm=arttm.rsplit(tmrultmp[-1],1)[0].strip()        tmrultmp=tmrultmp[:-1]    reptab=[('一十','1'),('十','1'),('九','9'),('八','8'),('七','7'),('六','6'),    ('五','5'),('四','4'),('三','3'),('二','2'),('一','1'),('零','0')]    for rt in reptab:        arttm = arttm.replace(rt[0], rt[1])    return time.strptime(arttm, tmrultmp)print uninformTM2("发表日期:2014-10-23 19:27:20 ", ":%Y-%m-%d %H:%M:%S")print uninformTM2("来源:LADYMAX时尚网 责任编辑: 时间:2014年10月24日 16:51  ", "时间:%Y年%m月%d日 %H:%M")print uninformTM2("2014-09-19 | 来源: ", "%Y-%m-%d|")print uninformTM2("2014-10-28   责任编辑:小个子    来源:闻康资讯网 ", "%Y-%m-%d责任")print uninformTM2("Post by 风尚 · 十月 27, 2014 ·", "·%m月 %d, %Y·")print uninformTM2("October 9, 2014", "%B %d, %Y")print uninformTM2("Oct 9, 2014", "%b %d, %Y")

0 0
原创粉丝点击