I have a string with format:
["('colA':'datA1', 'colB':'datB1', 'colC':'datC1')",
"('colA':'datA2', 'colB':'datB2', 'colC':'datC2')",
..........
"('colA':'datAn', 'colB':'datBn', 'colC':'datCn')]
And i need to obtain a dict:
[{'colA': 'datA1', 'colB': 'datB1', 'colC': 'datC1'},
{'colA': 'datA2', 'colB': 'datB2', 'colC': 'datC2'},
..........
{'colA': 'datAn', 'colB': 'datBn', 'colC': 'datCn'}]
If possible I need to do it with Python's own functions or at least without using loops, but I do not know if this is possible.
As @nexus66 suggested, here's a more streamlined version of that answer:
l = ["('colA':'datA1', 'colB':'datB1', 'colC':'datC1')",
"('colA':'datA2', 'colB':'datB2', 'colC':'datC2')",
"('colA':'datAn', 'colB':'datBn', 'colC':'datCn')"]
result = [dict(
item.replace("'", '').split(':')
for item in s[1:-1].split(', ')
)
for s in l]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments