如何更改 CSV 文件中的日期和时间格式(dd-mm-yyyy 和 dd-mm-yyyy) hh:mm:ss 转 mm/dd/yyyy 和hh:mm 使用 Python 吗?

发布于 2025-01-18 01:35:48 字数 139 浏览 0 评论 0原文

我要去约会&来自 API 的时间结果,格式为 dd-mm-yyyy & hh-mm-ss。我想在 Python 中将其转换为 mm-dd-yyyy 格式,最简单/最快的方法是什么?

我尝试过使用 strftime。然而,我无法得到它。

I'm getting a date & time result from an API in the format dd-mm-yyyy & hh-mm-ss. I want to convert this to an mm-dd-yyyy format in Python, what's the easiest/quickest way?

I have tried using strftime. However, I could not get it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2025-01-25 01:35:48

您可以将旧时间转换为日期时间对象,然后可以将其重新格式化为新格式。

>>> def format_timestamps(timestamps, old_format="%d-%m-%Y & %H-%M-%S", new_format="%m/%d/%Y & %H:%M"):
...     new_timestamps = []
...     for timestamp in timestamps:
...        old_datetime = datetime.datetime.strptime(timestamp, old_format)
...        new_datetime = old_datetime.strftime(new_format)
...        new_timestamps.append(new_datetime)
...    return new_timestamps
...
>>> format_timestamps(['31-03-2022 & 12-19-23'])
['03/31/2022 & 12:19']

You can convert your old times to datetime objects, which can then be reformatted into the new format.

>>> def format_timestamps(timestamps, old_format="%d-%m-%Y & %H-%M-%S", new_format="%m/%d/%Y & %H:%M"):
...     new_timestamps = []
...     for timestamp in timestamps:
...        old_datetime = datetime.datetime.strptime(timestamp, old_format)
...        new_datetime = old_datetime.strftime(new_format)
...        new_timestamps.append(new_datetime)
...    return new_timestamps
...
>>> format_timestamps(['31-03-2022 & 12-19-23'])
['03/31/2022 & 12:19']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文