Python-将日期的字符串表示形式转换为ISO 8601 [英] Python - Convert string representation of date to ISO 8601

查看:336
本文介绍了Python-将日期的字符串表示形式转换为ISO 8601的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何转换这样的字符串:

In Python, how can I convert a string like this:


2010年12月16日,星期四12:14:05 +0000

Thu, 16 Dec 2010 12:14:05 +0000

转换为ISO 8601格式,同时保留时区?

to ISO 8601 format, while keeping the timezone?

请注意原始日期是字符串,输出也应该是字符串,而不是 datetime 或类似的东西。

Please note that the orginal date is string, and the output should be string too, not datetime or something like that.

I

推荐答案

使用 dateutil

import dateutil.parser as parser
text = 'Thu, 16 Dec 2010 12:14:05 +0000'
date = parser.parse(text)
print(date.isoformat())
# 2010-12-16T12:14:05+00:00

这篇关于Python-将日期的字符串表示形式转换为ISO 8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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