excel欄位列轉換datax 所需json小工具
阿新 • • 發佈:2021-10-27
1 ''' 2 --*************************************************************** 3 --*指令碼名稱: excelToJson 4 --*功能: excel-》json 5 --*輸入資料:excel 帶列名倆列 6 --*輸出資料:json字串 7 --*作者: guominghuang 8 --*更新時間: 2020-5-15 15:00 9 --*************************************************************** 10 11 --*版本控制:版本號 提交人 提交日期 提交內容12 -- V1.0 guominghuang 2020-5-15 新增上線 13 ''' 14 import os 15 import numpy as np 16 import pandas as pd 17 import json 18 19 ''' 20 轉換函式 21 ''' 22 def transform(file): 23 excel = pd.read_excel(file) 24 # 將data轉換為list 25 tempList = np.array(excel).tolist() 26 myList = []27 28 for row in tempList: 29 tempDict = {} 30 tempDict["name"] = row[0] 31 tempDict["type"] = row[1] 32 myList.append(tempDict) 33 34 ouputJson = {"column": myList} 35 #將dict轉換成json 36 jsObj = json.dumps(ouputJson) 37 # 將json寫出到檔案 38 fileObject = open('jsonFile.json', 'w') 39 fileObject.write(jsObj) 40 fileObject.close() 41 42 ''' 43 獲取當前目錄下excelToJson.xls 44 ''' 45 file_list = os.listdir('.') 46 for file in file_list: 47 if(file == 'excelToJson.xls'): 48 transform(file)