天天看點

json檔案轉換成excel

1、環境

python2.7

2、庫

xlwt、demjson、json

3、code

# -*- coding: utf-8 -*-
import xlwt,demjson,json
if __name__ == \'__main__\':
    \'\'\'讀取excel\'\'\'
    x = 0 #初始值 橫軸
    y = 0 #初始值 數軸
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet(\'sheet 1\')

    with open(\'api.json\', \'r\') as t:
        data = t.read().decode(\'utf-8\')
        apilist = data.strip(\',\').split(\'+\')
        for v in apilist:
            string = v.replace("\'", \'"\')
            dictDat = demjson.decode(string.encode(\'utf-8\'))
            # print dictDat[u\'methodsName\']

            sheet.write(x,y,u\'内容\')
            sheet.write(x,y+1,dictDat[u\'desc\'])

            sheet.write(x+1,y,u\'URL\')
            sheet.write(x+1,y+1,dictDat[u\'path\'])

            ## 參數
            sheet.write(x+2,y,  u\'參數\')
            sheet.write(x+2,y+1,u\'預設值\')
            sheet.write(x+2,y+2,u\'類型\')
            sheet.write(x+2,y+3,u\'說明\')

            ## 開始添加參數
            dict1 = dictDat[u\'params\']
            vv = 1
            for k,v in dict1.items():
                sheet.write(x+2+vv, y,  k)
                v = ( u\'空\' if v == \'\' else v)
                ss = ( u\'字元串\' if v == u\'空\' else u\'數值\')
                sheet.write(x+2+vv, y+1,  v)
                sheet.write(x+2+vv, y+2,  ss)
                sheet.write(x+2+vv, y+3,  \'\')
                vv += 1
            x = x + 4 + len(dict1)
            wbk.save(\'api.xlsx\')      
json檔案轉換成excel