是否有一个库函数可以将以下格式的字符串转换为字典的python列表?
输入:
data[0][value]=123&data[0][Type]=Range&data[1][value]=345&data[1][Type]=Fixed
输出
data = [{'value':123,'Type':'Range'},{'value':456,'Type':'Fixed'}]
其中input为str类型,输出为list类型。
试试这个:
import re
inp = 'data[0][value]=123&data[0][Type]=Range&data[1][value]=345&data[1][Type]=Fixed'
matches = re.findall(r'\[([0-9]+)\]\[([a-zA-Z]+)\]=([a-zA-Z0-9]+)', inp)
out = {}
for match in matches:
index, key, value = match
if out.get(index) is None:
out[index] = {}
out[index][key] = value
data = out.values()
这会给你
[{'Type': 'Fixed', 'value': '345'}, {'Type': 'Range', 'value': '123'}]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句