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)