将 xs:duration 数据类型解析为 Python datetime.timedelta 对象?
根据标题,我正在尝试解析包含 xs:duration
数据类型。我想将其转换为Python < code>timedelta 对象,然后我可以在进一步的计算中使用它。
是否有任何内置方法可以执行此操作,类似于 strptime()
函数?如果没有,实现这一目标的最佳方法是什么?
As per the title, I'm trying to parse an XML file containing an xs:duration
data type. I'd like to convert that into a Python timedelta
object, which I can then use in further calculations.
Is there any built-in way of doing this, similar to the strptime()
function? If not, what is the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于我已经有一个关于我在问题中提出的问题的工作示例,为了完整起见,我将其发布在这里。如果有更好的答案出现,我会接受。
这可行,但无法正确处理月份长度或闰年。就我而言,这不是问题,但值得牢记。
Seeing as I already have a working example of what I asked in the question, I'll post it here for completeness. If any better answers come up I'll accept.
This works, but won't handle the month lengths or leap years correctly. For my purposes this isn't an issue, but it is worth keeping in mind.
这是一个老问题,但供将来参考:您可以为此使用
isodate.parse_duration()
。它返回一个与timedelta
兼容的类。请参阅 https://pypi.python.org/pypi/isodate
This is an old question, but for future reference: You can use
isodate.parse_duration()
for this. It returns a class that is compatible withtimedelta
.See https://pypi.python.org/pypi/isodate