python解析时间格式脚本

对于这种时间格式:發表於: 星期一 五月 28, 2012 6:59 am

import re
INPUT = "發表於: 星期一 五月 28, 2012 6:59 am    文章主題: 對《大話新聞》改組的誠心思考/蔬菜麵"
pattern = re.compile(r'[\d]+')
b = re.findall(pattern, INPUT)
a = INPUT.split(' ')
monthdict = {"一月": "01","二月": "02", "三月": "01", "四月": "04", "五月": "05", "六月": "06",
                "七月": "07",  "八月": "08",  "九月": "09",  "十月": "10",  "十一月": "11",  "十二月": "12"}
year = a[4]
month = monthdict[a[2]]
day = b[0]
if a[6] == 'pm':
    hour = int(b[2].encode('utf-8')) + 12
hour= b[2]
min = b[3]
OUTPUT = "%s-%s-%s %s:%s:00"% (year, month, day, hour, min)
print OUTPUT

对于这种正常的时间格式   http://www.cdnews.com.tw 2015-11-02 17:33:55

import re
INPUT="http://www.cdnews.com.tw 2015-11-02 17:33:55"
pattern = re.compile(r'[\d]+')
a = re.findall(pattern, INPUT)
year = a[0]
month = a[1]
day = a[2]
hour = a[3]
minute = a[4]
second = a[5]
OUTPUT = "%s-%s-%s %s:%s:%s" % (year,month,day,hour,minute,second)
print OUTPUT

 对于这种时间格式  發表於: 星期三 十二月 14, 2016 6:45 pm

import re
INPUT = "發表於: 星期三 十二月 14, 2016 6:45 pm"
pattern = re.compile(r'[\d]+')
b = re.findall(pattern, INPUT)
a = INPUT.split(' ')
monthdict = {"一月": "01","二月": "02", "三月": "01", "四月": "04", "五月": "05", "六月": "06","七月": "07",  "八月": "08",  "九月": "09",  "十月": "10",  "十一月": "11",  "十二月": "12"}
year = a[4]
month = monthdict[a[2]]
day = b[0]
if a[6] == 'pm':
    hour = int(b[2].encode('utf-8')) + 12
elif a[6] == 'am':
    h = int(b[2])
    if h >= 10:
        hour = h
    elif h<10:
        hour= "0"+b[2]
min = b[3]
OUTPUT = "%s-%s-%s %s:%s:00"% (year, month, day, hour, min)
print OUTPUT

 

转载于:https://www.cnblogs.com/junrong624/p/4930912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值