In [1]:
import ast

In [2]:
s = '["a", "b", "c"]'

In [3]:
l = ast.literal_eval(s)
print(l)


['a', 'b', 'c']

In [4]:
print(type(l))


<class 'list'>

In [5]:
s = '["x", 1, True]'

In [6]:
l = ast.literal_eval(s)
print(l)


['x', 1, True]

In [7]:
print(type(l[0]))


<class 'str'>

In [8]:
print(type(l[1]))


<class 'int'>

In [9]:
print(type(l[2]))


<class 'bool'>

In [10]:
s = '{"key1": 1, "key2": 2}'

In [11]:
d = ast.literal_eval(s)
print(d)


{'key1': 1, 'key2': 2}

In [12]:
print(type(d))


<class 'dict'>

In [13]:
s = '{1, 2, 3}'

In [14]:
se = ast.literal_eval(s)
print(se)


{1, 2, 3}

In [15]:
print(type(se))


<class 'set'>

In [16]:
s = '["x", 1 + 10]'

In [17]:
print(eval(s))


['x', 11]

In [18]:
# print(ast.literal_eval(s))
# ValueError: malformed node or string

In [19]:
a = 100
print(eval('[1, a]'))


[1, 100]

In [20]:
# a = 100
# print(ast.literal_eval('[1, a]'))
# ValueError: malformed node or string

In [21]:
import json

In [22]:
s = '{"key1": [1, 2, 3], "key2": "abc"}'

In [23]:
print(json.loads(s))


{'key1': [1, 2, 3], 'key2': 'abc'}

In [24]:
print(ast.literal_eval(s))


{'key1': [1, 2, 3], 'key2': 'abc'}

In [25]:
s = '[True, False, None]'

In [26]:
# print(json.loads(s))
# JSONDecodeError: Expecting value:

In [27]:
print(ast.literal_eval(s))


[True, False, None]

In [28]:
s = '[true, false, null]'

In [29]:
print(json.loads(s))


[True, False, None]

In [30]:
# print(ast.literal_eval(s))
# ValueError: malformed node or string

In [31]:
s = "{'key1': 'abc', 'key2': '''xyz'''}"

In [32]:
# print(json.loads(s))
# JSONDecodeError: Expecting property name enclosed in double quotes

In [33]:
print(ast.literal_eval(s))


{'key1': 'abc', 'key2': 'xyz'}

In [34]:
s = 'a, b, c'

In [35]:
l = s.split(', ')
print(l)


['a', 'b', 'c']

In [36]:
print(type(l))


<class 'list'>

In [37]:
s = '1-2-3'

In [38]:
l = s.split('-')
print(l)


['1', '2', '3']

In [39]:
print(type(l[0]))


<class 'str'>

In [40]:
l = [int(c) for c in s.split('-')]
print(l)


[1, 2, 3]

In [41]:
print(type(l[0]))


<class 'int'>