Вы можете использовать re.findall
с itertools.groupby
:
import re, itertools as it
def get_vals(d):
r = [(a, list(b)) for a, b in it.groupby(re.findall('\w+\=|[^\s,]+', d), key=lambda x:x[-1] == '=')]
return {r[i][-1][0][:-1]:', '.join(r[i+1][-1]) for i in range(0, len(r), 2)}
tests = ['key1=value1, key2=value2, key3=value3', 'key1=va, lue1, key2=valu, e2, test, key3=value3']
print(list(map(get_vals, tests)))
Выход:
[{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
{'key1': 'va, lue1', 'key2': 'valu, e2, test', 'key3': 'value3'}]