+ 3

Is there any way to typecast complex string to list?

Example. m_list = "[di1,di2,di3]" result = [di1,di2,di3] for complex data strings like list of lists using built in Python libraries.

14th Jun 2020, 6:28 PM
Muzhar Hussain
Muzhar Hussain - avatar
3 Antworten
+ 5
Not pure typecast. Split is powerfull to split text into lists. m_list = "[1, 2, 3]" m = [int(i) for i in m_list[1:-1].split(',')] print(m)
14th Jun 2020, 6:42 PM
Louis
Louis - avatar
+ 5
m_list = "[1, 2, 3]" m = eval(m_list) print(m)
14th Jun 2020, 6:55 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Use string replace and split methods. You can string string-methods together to make a one-liner. (hope that makes sense). You can also use the "re" module (regular expression).
14th Jun 2020, 7:53 PM
rodwynnejones
rodwynnejones - avatar