Create a separate function for converting the value to its proper type.
Take a look at How to convert list of strings to their correct Python types?, where the answers use either ast.literal_eval or json.loads (amongst other solutions) to deserialize a string to a suitable Python object:
import json
def convert(val):
try:
return json.loads(val)
except ValueError:
return val # return as-is
Then apply that function on each of the values from the original string.
def str_to_conf_dict(input_str):
d = {}
for pair in input_str.split(";"):
k, v = pair.split("=")
d[k] = convert(v)
return d
s = "a=b;b=2;c=-3;d=xyz;e=4ajkl;f=3.14"
print(str_to_conf_dict(s))
{'a': 'b', 'b': 2, 'c': -3, 'd': 'xyz', 'e': '4ajkl', 'f': 3.14}
All the numbers (ints, floats, and negative ones) should be converted to numbers, while others are retained as-is (as strings, with quotes).
If you want to (unnecessarily) force it into a one-liner (for some reason), you'll need to setup a {key : convert(value)} dictionary comprehension. You can either .split twice to get each item of the pair:
def str_to_conf_dict(input_str):
return {
pair.split('=')[0]: convert(pair.split('=')[1])
for pair in input_str.split(';')
}
Or pair up the items from the .split('=') output. You can take inspiration from the pairwise recipe from the itertools package, or implement something simpler if you know the format is always going to be key=val:
def get_two(iterable):
yield iterable[0], iterable[1]
def str_to_conf_dict(input_str):
return {
k: convert(v)
for pair in input_str.split(';')
for k, v in get_two(pair.split('='))
}