python ast.literal_eval and datetime

后端 未结 6 1356
终归单人心
终归单人心 2020-12-09 18:26

I have a string \"{\'datetime\': datetime.datetime(2010, 11, 21, 0, 56, 58)}\" and I want to convert this to the object it represents. Using ast.literal_

6条回答
  •  盖世英雄少女心
    2020-12-09 19:12

    I ran into this issue and solved it by replacing the datetime object with a string like this:

                if mystrobj.__contains__('datetime.datetime('):
                    mystrobj = re.sub(r"datetime.datetime([(),0-9 ]*),", r"'\1',", mystrobj)
    

提交回复
热议问题