开发者

Problem setting time.struct_time properties in Python

开发者 https://www.devze.com 2023-03-22 09:39 出处:网络
After I parsed a date in Python I need to patch it. But structure time.struct_time has read-only properties only:

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

精彩评论

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

关注公众号