如何在Python中解析HTTP日期字符串? [英] How do I parse an HTTP date-string in Python?

查看:220
本文介绍了如何在Python中解析HTTP日期字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中有一个简单的方法来解析HTTP日期字符串吗?根据标准,有几种格式化方式HTTP日期字符串;该方法应该能够处理这个。



换句话说,我想转换一个字符串,如Wed,23 Sep 2009 22:15:29 GMT到python time-structure。

解决方案

 >>> import email.utils as eut 
>>> eut.parsedate('Wed,23 Sep 2009 22:15:29 GMT')
(2009,9,23,22,15,29,0,1,-1)

如果你想要一个 datetime.datetime 对象,你可以这样做:


  def my_parsedate(text):
return datetime.datetime(* eut.parsedate(text)[:6])


Is there an easy way to parse HTTP date-strings in Python? According to the standard, there are several ways to format HTTP date strings; the method should be able to handle this.

In other words, I want to convert a string like "Wed, 23 Sep 2009 22:15:29 GMT" to a python time-structure.

解决方案

>>> import email.utils as eut
>>> eut.parsedate('Wed, 23 Sep 2009 22:15:29 GMT')
(2009, 9, 23, 22, 15, 29, 0, 1, -1)

If you want a datetime.datetime object, you can do:

def my_parsedate(text):
    return datetime.datetime(*eut.parsedate(text)[:6])

这篇关于如何在Python中解析HTTP日期字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆