对于这种时间格式:發表於: 星期一 五月 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 = {"一月": "","二月": "", "三月": "", "四月": "", "五月": "", "六月": "",
"七月": "", "八月": "", "九月": "", "十月": "", "十一月": "", "十二月": ""}
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 = {"一月": "","二月": "", "三月": "", "四月": "", "五月": "", "六月": "","七月": "", "八月": "", "九月": "", "十月": "", "十一月": "", "十二月": ""}
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= ""+b[2]
min = b[3]
OUTPUT = "%s-%s-%s %s:%s:00"% (year, month, day, hour, min)
print OUTPUT

python解析时间格式脚本的更多相关文章

  1. python中时间格式

    问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是:         'Thu, 19 Feb 2009 16:00:07 GMT'   (http呈现出来的格式) ...

  2. python:时间格式转化

    1.获取秒级时间戳与毫秒级时间戳.微秒级时间戳 import time import datetime t = time.time() print (t) #原始时间数据 print (int(t)) ...

  3. python format 时间格式

    trainData['survey_time'] = pd.to_datetime(trainData['survey_time'],format = '%Y/%m/%d %H:%M') trainD ...

  4. python 解析json格式

    对于网页爬取结果为json格式的,可以直接使用python的json库解析,获取相应字段的值,比用正则匹配更简单规范. import json…… resp=requests.post(url,hea ...

  5. Python日志输出格式和时间格式

    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H:%M:% ...

  6. python解析VOC的xml文件并转成自己需要的txt格式

    在进行神经网络训练的时候,自己标注的数据集往往会有数据量不够大以及代表性不强等问题,因此我们会采用开源数据集作为训练,开源数据集往往具有特定的格式,如果我们想将开源数据集为我们所用的话,就需要对其格式 ...

  7. python 爬虫时间数据-时间格式转换

    1 import time,datetime 2 3 time_original = '17/Sep/2012:11:40:00' 4 time_format = datetime.datetime. ...

  8. python中将HTTP头部中的GMT时间转换成datetime时间格式

    原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...

  9. Python 时间格式转换

    Python time, datetime模块常用方法 1.使用time模块,获取当前时间戳~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~import timetime.time( ...

随机推荐

  1. popViewControllerAnimated 后的刷新问题

    popViewControllerAnimated后,进入的viewcontroller不能即时刷新. 这时它不执行viewDidLoad,但执行viewWillAppear:(BOOL),所以只要把 ...

  2. Flask数据库常见关系模板代码

    常见关系模板代码 以下罗列了使用关系型数据库中常见关系定义模板代码 一对多 示例场景: 用户与其发布的帖子(用户表与帖子表) 角色与所属于该角色的用户(角色表与多用户表) 示例代码 class Rol ...

  3. mac svn: E210004: Number is larger than maximum

    SVN服务器IP地址发现改变,在Eclipse中的SVN资源库中执行Relocate重定位时发生错误: mac svn: E210004: Number is larger than maximum ...

  4. angular 中不要使用location.href

    location.href = '#/HKorderList?gid='+gid+'&gname='+encodeURIComponent(gname)+'&cPeriod='+$(' ...

  5. ECMAScript 6 基础入门

    0. ECMAScript 与 JavaScript ECMAScript (ECMA 262)是 JavaScript 语言标准的名字,ECMAScript 是标准,JavaScript 是各厂商的 ...

  6. Linux 查看版本號

    前言 剛剛升級了Fedora27,想要一查究竟.無奈竟不知如何查看linux版本,依稀記得uname好像可以,輸入之後就尷尬了. $ uname Linux 查看網上各種教程,看到一個不錯的博文,爲[ ...

  7. 诗·想——日记&断章

    日记      海子 姐姐,今夜我在德令哈,夜色笼罩 姐姐,我今夜只有戈壁 草原尽头我两手空空 悲痛时握不住一颗泪滴 姐姐,今夜我在德令哈 这是雨水中一座荒凉的城 除了那些路过的和居住的 德令哈... ...

  8. python中利用类创建的对象来保存信息

    在类创建的对象中,一般都是以字典的方式来保存信息 class Student: def __init__(self, name, age, score): self.name = name self. ...

  9. Jmeter聚合报告

    Label:请求的Name. #Samples:发出请求数量. Average:平均响应时间(单位:ms). Median:全部响应时间中位数,. 90%Line:90%用户的响应时间低于这个时间. ...

  10. Uva12230Crossing Rivers (数学期望)

    问题: You live in a village but work in another village. You decided to follow the straight path betwe ...