我有这种投入:转换.lua表Python字典
sometable = {
["a"] = {
"a1",
},
["b"] = {
"b1",
["b2"] = true,
},
["c"] = {
"c1",
["c2"] = true,
},
},
,并想将它转化成一些字典,我可以在python工作 - 或者基本上,我只需要能够读取这种模式中的数据:
print sometable[b][b2]
什么是最好的解决方案呢?我试着做了一堆取代了使用ast
,即把它转换:
def make_dict(input): # just body, ie. without 'sometable'
input = input.replace("=", ":")
input = input.replace("[\"", "\"")
input = input.replace("\"]", "\"")
input = input.replace("\t", "")
input = input.replace("\n", "")
input = "{" + input + "}"
return ast.literal_eval(input)
的问题是,输出是:
{
"a" :
{"a1", },
"b" :
{"b1", "b2" : true,},
"c" :
{"c1", "c2" : 1,},
}
错误(invalid syntax
)是{"b1", "b2" : true,},
。任何建议?
你要转换'“B1”''成“B1”:None'或类似的东西 - 字典不能没有价值的关键。 BTW'{“a1”,}'不是字典而是'set()' - 尝试'print(type {{a1“,})'',您会得到'' –
furas
如何将''b1”'转换为''b1':None''给定了上面我已经提到的函数?我假设一些模式匹配会被用来找到''something','并用'替换掉':None,'',但我不确定如何做到这一点 – emihir0
只是好奇,为什么你想用它与Python?:) – warspyking