zoukankan      html  css  js  c++  java
  • (转载) 游戏策划的excel配置表转成json文件(一)

    游戏客户端里一般无法读取策划写的excel配置表,需要先转成可以用的格式,例如json,xml格式。

    我用到的工具是python脚本,python脚本的强大就在这不提啦,各种牛X的成熟库。。。

    执行脚本如下: 

    [cpp] view plaincopy
     
    1. import os  
    2. import sys  
    3. import codecs  
    4. import json  
    5. from xlrd import open_workbook  
    6.   
    7.   
    8. # "<type 'unicode'>"  
    9. # "<type 'float'>"  
    10. def my_text_(text):  
    11.     """Return the translated text from json text."""  
    12.     v = ("<type '", "'>")  
    13.     if text[:len(v[0])] != v[0]: return text  
    14.     if text[-len(v[1]):] != v[1]: return text  
    15.     return text[len(v[0]) : -len(v[1])]  
    16.   
    17. def sheet2json(sheet, jsonfile):  
    18.   
    19.     row = 0  
    20.     attribute_row = []  
    21.     for col in range(sheet.ncols):  
    22.         attribute_row.append(sheet.cell(row, col).value)  
    23.   
    24.     attribute = {}  
    25.     row=1  
    26.     for col in range(sheet.ncols):  
    27.         attribute[attribute_row[col]] = my_text_(repr(type(sheet.cell_value(row, col))))  
    28.   
    29.     entities = []  
    30.     for row in range(2, sheet.nrows):  
    31.   
    32.         entity = {}  
    33.         for col in range(sheet.ncols):  
    34.             entity[attribute_row[col]] = sheet.cell(row, col).value  
    35.   
    36.         row_dict = {}  
    37.         row_dict["entity"] = entity  
    38.   
    39.         entities.append(row_dict)  
    40.   
    41.     sheet_dict = {}  
    42.     sheet_dict["entities"] = entities  
    43.   
    44.     filed = codecs.open("../../jsonsFromExcel/"+jsonfile, "w", "utf-8") #输出目录  
    45.     filed.write("%s" % json.dumps(sheet_dict, ensure_ascii=False, indent=4))  
    46.     #print json.dumps(sheet_dict, ensure_ascii=False, indent=4)  
    47.     filed.close()  
    48.   
    49.   
    50.   
    51. if len(sys.argv) != 2 :  
    52.     print "usage: %s [input xls file]" % sys.argv[0]  
    53.     print "e.g. : %s myinput.xls" % sys.argv[0]  
    54.     print "Note : the input file should be MS excel doc. "  
    55.   
    56.     sys.exit()  
    57.   
    58.   
    59. xls_file = sys.argv[1]  
    60.   
    61. workbook = open_workbook(xls_file)  
    62.   
    63. for sheet in workbook.sheets():  
    64.     jsonfile = sheet.name + ".json"  
    65.     #print "Converting %s's %s from EXCEL's sheet to JSON as %s" % (xls_file, sheet.name, jsonfile)  
    66.   
    67.     sheet2json(sheet, jsonfile)  

    用这个命令前需要先在系统里安装xlrd库,一个解析excel文件的第三方库

    原文地址:http://blog.csdn.net/zhuangyou123/article/details/10068729

  • 相关阅读:
    从《兄弟连》到团队管理
    将来
    [译] TypeScript入门指南(JavaScript的超集)
    基于cocos2dx迷宫游戏
    SVN版本管理教程
    arcgis for android 本地缓存
    vs2010变的特别卡解决办法
    cocos2d-x自适应屏幕
    cocos2d-x使用CCScale9Sprite
    cocos2dx开发入门文档
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/4228851.html
Copyright © 2011-2022 走看看