import os
import json
path = 'e:/a/'
for filename in os.listdir(path):
if filename.endswith('.lua'):
with open(path + filename) as f:
lines = f.readlines()
content = "".join(lines).replace("=", ":").replace("[", "").replace("]", "").replace(",", "").replace("{", "").replace("}", "")
content = content.split("\n")
data = {}
for line in content:
if line.strip() != "":
parts = line.strip().split(":")
key = parts[0].strip()
value = parts[1].strip()
if value.isdigit():
value = int(value)
data[key] = value
with open(path + filename[:-4] + '.json', 'w') as f:
f.write(json.dumps({"TestCfg": data}))
註意:本腳本僅作為參考,沒有對文件進行全面的錯誤處理,僅適用於樣例文件的格式。請在使用時仔細閱讀代碼,根據您的實際情況做適當的修改。