找回密码
 立即注册
查看: 520|回复: 1

[脚本] json转lua 格式python脚本

[复制链接]
发表于 2021-3-19 18:20 | 显示全部楼层 |阅读模式
  1. # coding=utf-8
  2. import re
  3. import sys

  4. #读取文件
  5. path = "test.json"
  6. if(len(sys.argv) > 1):
  7.     path = sys.argv[1]

  8. f = open(path)
  9. content = f.read()
  10. f.close()

  11. content = re.sub(r'\[', '{', content)

  12. content = re.sub(r']', '}', content)

  13. content = re.sub(r'//', '--', content)

  14. newList = []

  15. #找到所有键
  16. obj = re.compile( r'"[^,]*?:')
  17. list_1 = obj.findall(content)

  18. for s in list_1:
  19.     tmp = '[' + s
  20.     tmp = re.sub(r'"\s*?:', '"] =', tmp)
  21.     newList.append(tmp)

  22. #将处理后的字符串替换回去
  23. for i in range(len(list_1)):
  24.     content = content.replace(list_1[i], newList[i])

  25. #修正特殊情况,如"key" : "value", "key" : ...
  26. #content = re.sub(r'=\s*?\["', '= "', content)
  27. #content = re.sub(r',\s*"', ', ["', content)
  28. #print(content)

  29. #write to a lua file
  30. filename = re.sub(r'\..*,'', path)
  31. f = open(filename + '.lua', 'w')
  32. f.write(content)
  33. f.close()
  34. print("output --> " + filename + '.lua')
复制代码
 楼主| 发表于 2021-3-19 18:21 | 显示全部楼层
第二个参数传路径json 会在相同位置复制一份luatable的数据
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 09:32 , Processed in 0.096774 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表