游戏策划的excel配置表转成json文件(一)

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

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

执行脚本如下: 

import os
import sys
import codecs
import json
from xlrd import open_workbook


# "<type 'unicode'>"
# "<type 'float'>"
def my_text_(text):
    """Return the translated text from json text."""
    v = ("<type '", "'>")
    if text[:len(v[0])] != v[0]: return text
    if text[-len(v[1]):] != v[1]: return text
    return text[len(v[0]) : -len(v[1])]

def sheet2json(sheet, jsonfile):

    row = 0
    attribute_row = []
    for col in range(sheet.ncols):
        attribute_row.append(sheet.cell(row, col).value)

    attribute = {}
    row=1
    for col in range(sheet.ncols):
        attribute[attribute_row[col]] = my_text_(repr(type(sheet.cell_value(row, col))))

    entities = []
    for row in range(2, sheet.nrows):

        entity = {}
        for col in range(sheet.ncols):
            entity[attribute_row[col]] = sheet.cell(row, col).value

        row_dict = {}
        row_dict["entity"] = entity

        entities.append(row_dict)

    sheet_dict = {}
    sheet_dict["entities"] = entities

    filed = codecs.open("../../jsonsFromExcel/"+jsonfile, "w", "utf-8") #输出目录
    filed.write("%s" % json.dumps(sheet_dict, ensure_ascii=False, indent=4))
    #print json.dumps(sheet_dict, ensure_ascii=False, indent=4)
    filed.close()



if len(sys.argv) != 2 :
    print "usage: %s [input xls file]" % sys.argv[0]
    print "e.g. : %s myinput.xls" % sys.argv[0]
    print "Note : the input file should be MS excel doc.\n"

    sys.exit()


xls_file = sys.argv[1]

workbook = open_workbook(xls_file)

for sheet in workbook.sheets():
    jsonfile = sheet.name + ".json"
    #print "Converting %s's %s from EXCEL's sheet to JSON as %s" % (xls_file, sheet.name, jsonfile)

    sheet2json(sheet, jsonfile)

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值