2013-03-16 105 views
1

我已经读了几帖计算器,但仍然无法想出解决办法...字符串转换为时间与Python

我想爬Craigslist的职位过去48小时内公布。发布时间是在Craigslist的格式如下:

2013年3月15日,下午7点43分MDT

我已经试过

string = "2013-03-15, 7:43PM MDT" 

time.strptime(string, "%Y-%m-%d, %I:%M%p %Z") 

但明显犯规匹配字符串格式。这个时间字符串应该是什么格式?

+2

Check this post:http://stackoverflow.com/a/4766400/2086065 – longhua 2013-03-16 09:44:33

回答

1

问题是MDT。 Python的%Z不支持(至少在我看来)。有可能是更好的解决方案,但这一个应该工作:

import time 
import datetime 

#use the UTC which Python understands 
a="2013-03-15, 7:43PM MDT".replace("MDT","UTC") 
fs="%Y-%m-%d, %I:%M%p %Z" 
c=time.strptime(a, fs) 

#converting from UTC to MDT (time difference) 
dt = datetime.datetime.fromtimestamp(time.mktime(c)) - datetime.timedelta(hours=6) 
print dt 
相关问题