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

    参考链接:

    https://www.cnblogs.com/zjzyh/p/5769069.html

    https://blog.csdn.net/Nick_Li_/article/details/90139383

    代码如下:

    excel2json.py

     1 import xlrd
     2 import collections
     3 import json
     4 import codecs
     5 
     6 # excelPath:读取的excel路径
     7 # jsonPath:输出的json路径
     8 # keyRow:key所处的行
     9 # typeRow:type所处的行
    10 # valueRow:value开始的行
    11 def excel2json(excelPath, jsonPath, keyRow=1, typeRow=2, valueRow=3):
    12     wb = xlrd.open_workbook(excelPath)
    13     sheet = wb.sheet_by_index(0)
    14     keys = sheet.row_values(keyRow)
    15     types = sheet.row_values(typeRow)
    16     data = {}
    17     
    18     for row in range(valueRow,sheet.nrows):
    19         rowData = sheet.row_values(row)
    20         temp = collections.OrderedDict()
    21         for col in range(sheet.ncols):
    22             key = keys[col]
    23             colType = types[col]
    24             value = rowData[col]
    25             if colType == 'json':
    26                 temp[key] = json.loads(value)
    27             elif colType == 'int':
    28                 temp[key] = int(value)
    29             else: #string float在读取excel时已区分
    30                 temp[key] = value
    31         data[int(rowData[0])] = temp
    32 
    33     jsonStr = json.dumps(data,ensure_ascii=False,indent=4)
    34     with codecs.open(jsonPath,"w","utf-8") as f:
    35         f.write(jsonStr)
    36 
    37     return jsonStr

    测试:

    test.py

    1 import excel2json
    2 import json2lua
    3 import json
    4 
    5 jsonStr = excel2json.excel2json("./xls/zz.xls", "./json/zz.json")
    6 print(jsonStr)

    表格:

    输出:

     1 {
     2     "2": {
     3         "id": 2,
     4         "name": "",
     5         "desc": [
     6             [
     7                 1,
     8                 2
     9             ],
    10             [
    11                 3,
    12                 4
    13             ],
    14             5,
    15             [
    16                 6,
    17                 7
    18             ]
    19         ],
    20         "para": 1.2
    21     },
    22     "4": {
    23         "id": 4,
    24         "name": "",
    25         "desc": {
    26             "id": 5,
    27             "num": [
    28                 1
    29             ]
    30         },
    31         "para": 3.45
    32     },
    33     "6": {
    34         "id": 6,
    35         "name": "",
    36         "desc": [
    37             {
    38                 "a": 10
    39             }
    40         ],
    41         "para": 6.0
    42     }
    43 }
  • 相关阅读:
    win10笔记本实现双屏显示的自如切换
    word-如何将文字设置为插入超链接
    opencv-python教程学习系列13-图像平滑
    第12课 经典问题解析一
    第11课 新型的类型转换
    第10课 C++中的新成员
    第9课 函数重载分析(下)
    第8课 函数重载分析(上)
    第7课 函数参数的扩展
    第6课 内联函数分析
  • 原文地址:https://www.cnblogs.com/lyh916/p/11300014.html
Copyright © 2011-2022 走看看