实现统一时间格式的函数

#-*- coding:utf-8 -*-
import time

def 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值