我对你的问题的解决方案不需要导入任何模块:
my_input = ["('colA':'datA1', 'colB':'datB1', 'colC':'datC1')",
"('colA':'datA2', 'colB':'datB2', 'colC':'datC2')",
"('colA':'datAn', 'colB':'datBn', 'colC':'datCn')"]
global_list =[]
for i in my_input:
intern_dict = {}
for j in i.replace("(", "").replace(")", "").split(","):
c = j.split(":")
intern_dict["".join(c[0].replace("'", "").strip())] = "".join(c[1].replace("'","").strip())
global_list.append(intern_dict)
print("Type of global_list: {0}\n".format(type(global_list)))
print(global_list)
print("\nType of elements inside global_list\n")
for i in global_list:
print("{0}\tType: {1}".format(i, type(i)))
输出:
Type of global_list: <class 'list'>
[{'colA': 'datA1', 'colC': 'datC1', 'colB': 'datB1'}, {'colA': 'datA2', 'colC': 'datC2', 'colB': 'datB2'}, {'colA': 'datAn', 'colC': 'datCn', 'colB': 'datBn'}]
Type of elements inside global_list
{'colA': 'datA1', 'colC': 'datC1', 'colB': 'datB1'} Type: <class 'dict'>
{'colA': 'datA2', 'colC': 'datC2', 'colB': 'datB2'} Type: <class 'dict'>
{'colA': 'datAn', 'colC': 'datCn', 'colB': 'datBn'} Type: <class 'dict'>