我需要将一个字符串解析为python datetime,该字符串以这种格式接收-Thu Jul 15 12:57:35 AWST 2021
我使用下面的代码来解析它
datetime.datetime.strptime("Thu Jul 15 12:57:35 AWST 2021", "%a %b %d %H:%M:%S %Z %Y")
它触发错误-“值错误:时间数据'Thu Jul 15 12:57:35 AWST 2021'与格式'%a%b%d%H:%M:%S%Z%Y'不匹配”
问题是根据doc的说法,使用%Z无法识别时区AWST
%Z
In strftime(), %Z is replaced by an empty string if tzname() returns None; otherwise %Z is replaced by the returned value, which must be a string.
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
如何正确解析此字符串?顺便说一句,我使用的是python 3.8.2
提前谢谢
处理时区可能有点棘手。一种选择是从https://www.timeanddate.com/time/zones/绘制感兴趣时区的地图,然后使用
dateutils.parser
解析日期。 比如:使用UTC偏移量:
输出:
使用IANA时区名称: (如@MrFuppes在评论中所建议)
输出:
相关问题 更多 >
编程相关推荐