将字符串值从 pickled 转换为字典

convert string value to dictionary from pickled

val = "{t:30, f:50}" 

是一个字符串值,我需要将其转换为字典,而不是使用 val.split(',') 的常规方法,然后删除括号并取出键并将值绑定到它并转换它在字典里。谁能提出更好的方法。 PLz 确实关心即使在键(t 和 s)中的字符串中没有引号。从 db.Already 尝试 json 加载或转储中获得一些值。

import re
x="{t:30, f:50}"
y=re.findall(r"([^ {,]*):([^ {,]*)[,}]",x)
print dict(y)

尝试 this.Simple 并在一两步内完成。

import re
val = "{t:30, f:50}"
t = re.search("[^{].*[^}]",val).group()
print (t)
z = t.split(",")
print (z)
mydict = {}
mydict[z[0][0]]=z[0][2]+z[0][3]
print (mydict)

>>> 
t:30, f:50
['t:30', ' f:50']
{'t': '30'}
>>> 

使用re模块的search()方法