AnswerBun.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!

Related Questions

Range input controls two different fields

1  Asked on December 16, 2021 by mscmdaddcts

   

How do I find a duplicate in SQL

3  Asked on December 16, 2021 by jason-livengood

     

C++ nested class inside of a templated class

0  Asked on December 16, 2021 by dentiny

     

how to interpret “shift/reduce” conflicts in bison

3  Asked on December 16, 2021 by adi-mehrotra

   

Can you create a data frame of a dictionary?

1  Asked on December 16, 2021 by greg-sullivan

     

how to implement restapi in laravel and its working

1  Asked on December 16, 2021 by kulweet_kumar

   

How to find the biggest VALUE (not a key) in a HashMap?

1  Asked on December 16, 2021 by marika

       

Get value of editable tag through oninput function?

3  Asked on December 16, 2021 by swarnabho-biswas-mac

 

Replace patterns separated by delimiter in R

3  Asked on December 16, 2021

 

How can i sort a function?

2  Asked on December 16, 2021 by lumknight

       

‘MyList’ object does not support item assignment

5  Asked on December 16, 2021 by vinicius-miki

     

How can I move a item in a numpy array?

1  Asked on December 16, 2021 by lostsoul

   

Ask a Question

Get help from others!

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