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)
精彩评论