python - 如何在 Python 中解析 HTTP 日期字符串?

标签 python http datetime parsing

有没有一种简单的方法可以在 Python 中解析 HTTP 日期字符串?根据the standard ,有几种方法可以格式化 HTTP 日期字符串;该方法应该能够处理这个问题。

换句话说,我想将“Wed, 23 Sep 2009 22:15:29 GMT”之类的字符串转换为 python 时间结构。

最佳答案

>>> 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])

关于python - 如何在 Python 中解析 HTTP 日期字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1471987/

相关文章:

http - 是否有 MIME 类型告诉浏览器 "don' t 尝试检测类型,仅显示为文本”?

linux - 如何水平缩放http请求?

C# - 检查给定日期是否是一个月的第五周?

tsql - 在 T-SQL 中将日期时间截断为秒(删除毫秒)

python - 在 Python 中实现 Trie

python - 以下代码中的 'as' 关键字在做什么?

java - Spring RestTemplate getForObject 获取 404

php - 在 PHP 和 javascript 中使用 time() 和 setTime() 函数

python - 为什么 python 和我的网络浏览器对同一链接显示不同的代码?

python - 如何使用biopython提取和修剪fasta序列