Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert str() to list() or tuple() without losing quotes

Good afternoon!
I ran into a problem that on a "pure" python I can’t correctly convert a string of the form:

'("dbfd8379-f50c-4555-bc04-c138413cd27a", "2022-10-06 15:44:46.713", 1, "String for test1")'

To the list or tuple of the form:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

["dbfd8379-f50c-4555-bc04-c138413cd27a", "2022-10-06 15:44:46.713", 1, "String for test1"]

("dbfd8379-f50c-4555-bc04-c138413cd27a", "2022-10-06 15:44:46.713", 1, "String for test1")

I’ve tried a lot of options, mostly with replace, strip and split. But all of them did not give the desired result.
I really ask for your help. Please help me to figure it out.

>Solution :

Did you try with ast.literal_eval()?

text = '("dbfd8379-f50c-4555-bc04-c138413cd27a", "2022-10-06 15:44:46.713", 1, "String for test1")'

import ast
ast.literal_eval(text)

Output:

('dbfd8379-f50c-4555-bc04-c138413cd27a',
 '2022-10-06 15:44:46.713',
 1,
 'String for test1')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading