TransWikia.com

in Python turn bracketed, comma delimited string that contains a datetime object into a list

Stack Overflow Asked on January 3, 2022

This looks easy. Maybe it is and I’m not seeing it. How would one turn the following string into a list in Python3?

"[(datetime.datetime(2020, 7, 24, 19, 6, 54), '1002', '3.1'), (datetime.datetime(2020, 7, 24, 19, 5, 10), '1001', '1.5')]"

I would like a list that looks identical to above without the double quotation marks.

One Answer

This seems to be a legitimate python object:

a = "[(datetime.datetime(2020, 7, 24, 19, 6, 54), '1002', '3.1'), (datetime.datetime(2020, 7, 24, 19, 5, 10), '1001', '1.5')]"

import datetime
eval(a)
[(datetime.datetime(2020, 7, 24, 19, 6, 54), '1002', '3.1'), (datetime.datetime(2020, 7, 24, 19, 5, 10), '1001', '1.5')]

Answered by Onyambu on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP