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 }
  • 相关阅读:
    有关Python,网络,机器学习,深度学习
    Python map使用
    左旋转字符串,翻转字符串
    使用Python创建二叉树,作为调试程序使用
    batchnorm2d函数理解,numpy数据归一化
    和为s的连续正数序列,和为s的两个数字
    判断是否为平衡二叉树
    原生js格式化json的方法
    ace editor 使用教程
    Vue+webpack+echarts+jQuery=demo
  • 原文地址:https://www.cnblogs.com/lyh916/p/11300014.html
Copyright © 2011-2022 走看看