将军事时间从文本文件转换为标准时间 Python

发布于 2024-11-06 12:10:30 字数 80 浏览 3 评论 0原文

我在如何将军事时间从文本文件转换为标准时间并丢弃所有错误值方面遇到逻辑问题。我只到了要求用户提供输入文件并从输入的文本文件中显示内容的程度。请帮我

I am having problems with logic on how to convert military time from a text file to standard time and discard all the wrong values. I have only got to a point where the user is asked for the input file and the contents are displayed from the text file entered. Please help me

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

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

发布评论

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

评论(3

从此见与不见 2024-11-13 12:10:30

Python 的 datetime.time 对象使用“军事” “ 时间。您可以执行以下操作:

>>> t = datetime.time(hour=15, minute=12)
>>> u = datetime.time(hour=16, minute=44)
>>> t = datetime.datetime.combine(datetime.datetime.today(), t)
>>> t
datetime.datetime(2011, 5, 11, 15, 12)
>>> u = datetime.datetime.combine(datetime.datetime.today(), u)
>>> t - u
datetime.timedelta(-1, 80880)

稍微调整一下,您所描述的转换应该非常简单。


在没有看到任何代码的情况下,很难说出你到底想要什么。但我假设您可以执行以下操作:

raw_time = '2244'
converted_time = datetime.time(hour=int(raw_time[0:2]), minute=int(raw_time[2:4]))
converted_time = datetime.datetime.combine(datetime.datetime.today(), converted_time)

现在您可以使用 converted_time,添加和减去 timedelta 对象。仅供参考,您可以像这样创建一个 timedelta

td = datetime.timedelta(hours=4)

timedelta 构造函数的可能关键字参数的完整列表为 此处

Python's datetime.time objects use "military" time. You can do things like this:

>>> t = datetime.time(hour=15, minute=12)
>>> u = datetime.time(hour=16, minute=44)
>>> t = datetime.datetime.combine(datetime.datetime.today(), t)
>>> t
datetime.datetime(2011, 5, 11, 15, 12)
>>> u = datetime.datetime.combine(datetime.datetime.today(), u)
>>> t - u
datetime.timedelta(-1, 80880)

With a little twiddling, conversions like the ones you describe should be pretty simple.


Without seeing any code, it's hard to tell what exactly you want. But I assume you could do something like this:

raw_time = '2244'
converted_time = datetime.time(hour=int(raw_time[0:2]), minute=int(raw_time[2:4]))
converted_time = datetime.datetime.combine(datetime.datetime.today(), converted_time)

Now you can work with converted_time, adding and subtracting timedelta objects. Fyi, you can create a timedelta like so:

td = datetime.timedelta(hours=4)

The full list of possible keyword arguments to the timedelta constructor is here.

踏雪无痕 2024-11-13 12:10:30
from dateutil.parser import parse
time_today = parse("08:00")

from dateutil.relativedelta import relativedelta
required_time = time_today-relativedelta(minutes=35)

print required_time
datetime.datetime(2011, 5, 11, 7, 25)
from dateutil.parser import parse
time_today = parse("08:00")

from dateutil.relativedelta import relativedelta
required_time = time_today-relativedelta(minutes=35)

print required_time
datetime.datetime(2011, 5, 11, 7, 25)
烟沫凡尘 2024-11-13 12:10:30

这不是像其他两个那样的真实答案,而是我在 python 中处理日期和时间时使用的哲学:从用户那里获取后尽快转换为日期时间对象,并且仅在呈现时转换回字符串将价值回馈给用户。

您需要的大多数日期/时间数学可以通过 datetime 对象及其近亲 timedelta 对象来完成。除非您需要时间增量与其他时间增量的比率,但那是另一个故事了。

It's not a true answer like the other two, but the philosophy I use when dealing with dates and times in python: convert to a datetime object as soon as possible after getting from the user, and only convert back into a string when you are presenting the value back to the user.

Most of the date/time math you will need can be done by datetime objects, and their cousins the timedelta objects. Unless you need ratios of timedeltas to other timedeltas, but that's another story.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文