开发者

Problem setting time.struct_time properties in Python

After I parsed a date in Python I need to patch it. But structure time.struct_time has read-only properties only:

parsed = time.strptime("23:59", "%H:%M")
parsed.tm_year = 2011
> TypeError: readonly attribute
开发者_运维知识库

How do I get a patched datetime value in a short & clever way?


Use datetime:

>>> p = datetime.datetime.strptime("23:59", "%H:%M")
>>> p = p.replace(year=2011)
>>> p
datetime.datetime(2011, 1, 1, 23, 59)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜