【问题标题】:Convert date value in a list转换列表中的日期值
【发布时间】:2021-09-01 10:01:01
【问题描述】:

我有一个包含这种格式的日期值的列表(datetime.datetime(2021, 9, 1, 8, 30, 25, 430000, tzinfo=tzlocal()))。我想将此日期值转换为 ISO 格式。目的是 CloudWatch Log Insight 无法读取此日期格式。

输出:

[{'timestamp': datetime.datetime(2021, 9, 1, 8, 30, 25, 430000, tzinfo=tzlocal()), 'type': 'ExecutionFailed', 'id': 22, 'previousEventId': 21, 'executionFailedEventDetails': {'error': 'States.TaskFailed', 'cause': '{"AllocatedCapacity":2,"Attempt":0,"CompletedOn":1630485024021,"ErrorMessage":"An error occurred while calling o80.getDynamicFrame. The TCP/IP connection to the host 172.31.17.102, port 1433 has failed. Error: \\"Connection timed out: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.\\".","ExecutionTime":100,"GlueVersion":"2.0","Id":"jr_504a3acc2cdb7e3e18d2d22d8df69747744030e24afbcbee7f02de1ece6599c3","JobName":"sqlserver-ingest","JobRunState":"FAILED","LastModifiedOn":1630485024021,"LogGroupName":"/aws-glue/jobs","MaxCapacity":2.0,"NumberOfWorkers":2,"PredecessorRuns":[],"StartedOn":1630484906355,"Timeout":2880,"WorkerType":"G.1X"}'}}]

【问题讨论】:

  • 向我们展示输入数据...
  • 到目前为止你有什么尝试?
  • for i in lst: i['timestamp'] = i['timestamp'].isoformat()…?
  • 这能回答你的问题吗? Datetime to ISO 8601 in Python

标签: python


【解决方案1】:

使用datetime.isoformat()

import datetime
from dateutil.tz import tzlocal

dt = datetime.datetime(2021, 9, 1, 8, 30, 25, 430000, tzinfo=tzlocal())
dt_iso = dt.isoformat()
print(type(dt_iso), dt_iso)
<class 'str'> 2021-09-01T08:30:25.430000+00:00

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多