Python制作excel表转json

最近项目需要一个excel转json的工具,找了很多,感觉都不合适,最后还是参考网上做一个符合自己要求的。
参考:这里
先附上源码:

import xlrd, re, json, os, codecs

sourcePath = '.\\source'
jsonPath = '.\\json'

reg = re.compile( '^(\w*)$' )
regArr1 = re.compile( r'''
    ^(\w*)
    \[(\w*)\]$
    ''', re.VERBOSE )
regArr2 = re.compile( r'''
    ^(\w*)
    \[(\d*)\]
    <(\w*)>$''', re.VERBOSE )
nType = 0

def XlsToJson( filePath, savePath ):
    print( 'filePath: ' + filePath + ' savePath: ' + savePath )
    data = xlrd.open_workbook( filePath )
    table = data.sheet_by_index( 0 )
    commentInfo = []
    headerInfo = []
    xlsInfo = {}
    dataInfo = {}
    ids = []

    dataInfo["dic"] = xlsInfo
    for i in range( table.ncols ):
        commentInfo.append( table.cell( 0, i ).value )
        header = table.cell( 1, i ).value
        headerInfo.append( header )
        match = regArr2.match( header )
        if match:
            nType = 3
        else:
            match = regArr1.match( header )
            if match:
                nType = 2
            else:
                match = reg.match( header )
                nType = 1
        nId = 0
        for j in range( 2, table.nrows ):
            cellInfo = table.cell( j, i ).value
            if cellInfo != "":
                if i == 0:
                    cellInfo = int( cellInfo )
                    ids.append( cellInfo )
                    xlsInfo[cellInfo] = {}
                nId = int(ids[j - 2])
                if nType == 1:
                    xlsInfo[nId][match.group( 1 )] = cellInfo
                elif nType == 2:
                    if match.group( 1 ) not in xlsInfo[nId]:
                        xlsInfo[nId][match.group( 1 )] = {}
                    xlsInfo[nId][match.group( 1 )][int( match.group( 2 ) )] = cellInfo
                elif nType == 3:
                    if match.group( 1 ) not in xlsInfo[nId]:
                        xlsInfo[nId][match.group( 1 )] = []
                    if len( xlsInfo[nId][match.group( 1 )] ) < int( match.group( 2 ) ):
                        xlsInfo[nId][match.group( 1 )].append( {} )
                    xlsInfo[nId][match.group( 1 )][int( match.group( 2 ) ) - 1][match.group( 3 )] = cellInfo

    outFile = codecs.open( savePath, 'w', 'utf-8' )

    json.dump( dataInfo, outFile, ensure_ascii=False, indent = 4, sort_keys = True)
    outFile.write('\n')

for root, dirs, files in os.walk( sourcePath ):
    for file in files:
        if file.endswith( '.xlsx' ):
            XlsToJson( os.path.join( root, file ), os.path.join( jsonPath, os.path.basename( file ).replace( '.xlsx', '.json' ) ) )

我改了一下,本来有空数据的情况也会导也json数据,我感觉这是无效数据,不符合我的要求,导出来的数据是json类型的还要自己解析,这样也太麻烦了,我改成json的字典类型。这样只要映射类就可以了,不用管数据多少,都映射出来,这样可以方便使用。
附上工具:
[excel转Json]("链接:https://pan.baidu.com/s/1tyAsy8EK91WSgHA12Ky1sw 密码:llkd")

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,057评论 3 119
  • zhouios阅读 4,576评论 0 0
  • 到底人和人的吸引是怎么产生的呢?有的人他会比较的有吸引力,看一眼就让人不知觉的想要靠近过去,他们身上有什么比较特别...
    科霖大叔阅读 3,318评论 0 0
  • 黄昏给予我的印象始于小学课本的一篇文章——《火烧云》。 在四面环山的小村庄,晴朗的天里几乎都能欣赏夕阳西下的美妙。...
    wy文意阅读 3,666评论 2 4
  • 今天天气好,外出爬山,看着阳光灿烂很想画画,于是赶紧拍了几张就回家动手,画了一幅,真想立马删掉。。。考虑到孩子再丑...
    天马行空的林子阅读 2,622评论 4 1

友情链接更多精彩内容