json转lua 格式python脚本
# coding=utf-8import re
import sys
#读取文件
path = "test.json"
if(len(sys.argv) > 1):
path = sys.argv
f = open(path)
content = f.read()
f.close()
content = re.sub(r'\[', '{', content)
content = re.sub(r']', '}', content)
content = re.sub(r'//', '--', content)
newList = []
#找到所有键
obj = re.compile( r'"[^,]*?:')
list_1 = obj.findall(content)
for s in list_1:
tmp = '[' + s
tmp = re.sub(r'"\s*?:', '\"] =', tmp)
newList.append(tmp)
#将处理后的字符串替换回去
for i in range(len(list_1)):
content = content.replace(list_1, newList)
#修正特殊情况,如"key" : "value", "key" : ...
#content = re.sub(r'=\s*?\[\"', '= \"', content)
#content = re.sub(r',\s*\"', ', [\"', content)
#print(content)
#write to a lua file
filename = re.sub(r'\..*,'', path)
f = open(filename + '.lua', 'w')
f.write(content)
f.close()
print("output --> " + filename + '.lua') 第二个参数传路径json 会在相同位置复制一份luatable的数据
页:
[1]