Converting .lua table to a python dictionary查询将 lua 表转换为可以使用 loadstring/loadfile 加载的 python 字典。 answer's建议使用一个库,它也支持相反的转换,但它不再维护也不支持 python3。
我无法在任何地方找到执行此转换的代码。
最佳答案
我最终自己实现了它:
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)}")
关于python - 将 dict 序列化为 lua 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54392760/