Converting .lua table to a python dictionary查询有关将lua表转换为可通过loadstring / loadfile加载的python dict的问题。 answer's建议使用一个库,该库也支持相反的转换,但是不再维护也不支持python3。
我找不到在任何地方进行此转换的代码。
答案 0 :(得分:1)
我最终自己实现了它:
def dump_lua(data):
if type(data) is str:
return f'"{re.escape(data)}"'
if type(data) in (int, float):
return f'{data}'
if type(data) is bool:
return data and "true" or "false"
if type(data) is list:
l = "{"
l += ", ".join([dump_lua(item) for item in data])
l += "}"
return l
if type(data) is dict:
t = "{"
t += ", ".join([f'[\"{re.escape(k)}\"]={dump_lua(v)}'
for k,v in data.items()])
t += "}"
return t
logging.error(f"Unknown type {type(data)}")