zoukankan      html  css  js  c++  java
  • [Unity工具]python导表工具03:json转lua

    参考链接:

    https://blog.csdn.net/liangneo/article/details/42461509

    代码如下:

    json2lua.py

     1 import json
     2 import types
     3 import json
     4 import os
     5 import codecs
     6 
     7 def space_str(layer):
     8     lua_str = ""
     9     for i in range(0,layer):
    10         lua_str += '	'
    11     return lua_str
    12 
    13 def dic_to_lua_str(data,layer=0):
    14     d_type = type(data)
    15     if d_type is str:
    16         return "'" + data + "'"
    17     elif d_type is bool:
    18         if data:
    19             return 'true'
    20         else:
    21             return 'false'
    22     elif d_type is int or d_type is float:
    23         return str(data)
    24     elif d_type is list:
    25         lua_str = "{
    "
    26         lua_str += space_str(layer+1)
    27         for i in range(0,len(data)):
    28             lua_str += dic_to_lua_str(data[i],layer+1)
    29             if i < len(data)-1:
    30                 lua_str += ','
    31         lua_str += '
    '
    32         lua_str += space_str(layer)
    33         lua_str +=  '}'
    34         return lua_str
    35     elif d_type is dict:
    36         lua_str = ''
    37         lua_str += "
    "
    38         lua_str += space_str(layer)
    39         lua_str += "{
    "
    40         data_len = len(data)
    41         data_count = 0
    42         for k,v in data.items():
    43             data_count += 1
    44             lua_str += space_str(layer+1)
    45             if type(k) is int:
    46                 lua_str += '[' + str(k) + ']'
    47             else:
    48                 lua_str += k 
    49             lua_str += ' = '
    50             try:
    51                 lua_str += dic_to_lua_str(v,layer +1)
    52                 if data_count < data_len:
    53                     lua_str += ',
    '
    54  
    55             except Exception:
    56                 print ('error in ',k,v)
    57                 raise
    58         lua_str += '
    '
    59         lua_str += space_str(layer)
    60         lua_str += '}'
    61         return lua_str
    62     else:
    63         print (d_type , 'is error')
    64         return None
    65 
    66 def str_to_lua_table(jsonStr):
    67     data_dic = None
    68     try:
    69         data_dic = json.loads(jsonStr)
    70     except Exception:
    71         data_dic =[]
    72     else:
    73         pass
    74     finally:
    75         pass
    76     bytes = ''
    77     for it in dic_to_lua_str(data_dic):
    78         bytes += it
    79     return bytes
    80 
    81 def file_to_lua_file(jsonFile,luaFile):
    82     with codecs.open(luaFile,"w","utf-8") as luafile:
    83         with codecs.open(jsonFile,"r","utf-8") as jsonfile:
    84             luafile.write(str_to_lua_table(jsonfile.read()))

    测试:

    test.py

     1 import excel2json
     2 import json2lua
     3 
     4 jsonStr = excel2json.excel2json("./xls/test.xls","./json/test.json",1,3)
     5 # print(jsonStr)
     6 
     7 # print(type({}),end='
    ')
     8 # print(type([]),end='
    ')
     9 # print(type(""),end='
    ')
    10 # print(type(1),end='
    ')
    11 # print(type(1.2),end='
    ')
    12 # print(type(True),end='
    ')
    13 # print(type(False),end='
    ')
    14 # print(type(None),end='
    ')
    15 print(json2lua.str_to_lua_table(jsonStr))
    16 json2lua.file_to_lua_file("./json/test.json","./lua/test.lua")

    json文件

    lua文件

  • 相关阅读:
    [转]Modernizr的介绍和使用
    java动态代理使用详解
    ajax上传文件以及使用中常见问题处理
    cmd下查询端口占用以及根据进程id名称结束进程
    水平居中 垂直居中
    inline-block和float
    一起入门前端(三)
    一起入门前端(二)
    一起入门前端(一)
    WPF初学——自定义样式
  • 原文地址:https://www.cnblogs.com/lyh916/p/11343266.html
Copyright © 2011-2022 走看看