将军事时间从文本文件转换为标准时间 Python
我在如何将军事时间从文本文件转换为标准时间并丢弃所有错误值方面遇到逻辑问题。我只到了要求用户提供输入文件并从输入的文本文件中显示内容的程度。请帮我
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 的
datetime.time
对象使用“军事” “ 时间。您可以执行以下操作:稍微调整一下,您所描述的转换应该非常简单。
在没有看到任何代码的情况下,很难说出你到底想要什么。但我假设您可以执行以下操作:
现在您可以使用
converted_time
,添加和减去timedelta
对象。仅供参考,您可以像这样创建一个timedelta
:timedelta
构造函数的可能关键字参数的完整列表为 此处。Python's
datetime.time
objects use "military" time. You can do things like this: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:
Now you can work with
converted_time
, adding and subtractingtimedelta
objects. Fyi, you can create atimedelta
like so:The full list of possible keyword arguments to the
timedelta
constructor is here.这不是像其他两个那样的真实答案,而是我在 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.