• 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
  • 相关阅读:
    1065 a+b and c(64)
    1049 counting ones
    1040 the longest symmetric
    1039 course list for student
    1038 recover the smallest number
    1035 head of a gang
    1033 to fill or not to fill
    node环境下通过redis共享session记录
    vue+koa+sequlize 搭建使程序员专注业务代码开发框架---对于nunjucks引入webpack后,产生的文件缓存相关的思考(四)
    charless抓包https---记录一下菜鸡的日常
  • 原文地址:https://www.cnblogs.com/junrong624/p/4930912.html
Copyright © 2020-2023  润新知