话说求人不如求己,在几番交涉无果之后,决定自己搞起来;刚好之前一直在用python,索性就用python语言来实现;
主要功能:读取json内容的txt文件,转化成excel格式保存;
整个编写过程还算顺利,遇到一个小问题,记录一下:
在解析json内容时,报错ValueError : No JSON object could be decoded
后来发现Python中Json库不支持带BOM的UTF-8编码格式,将txt文档的编码改为无BOM格式的UTF-8编码,再次运行即可成功;
具体源码如下,其中引用到了自己内部编写的公共类file:
#!/usr/bin/env python
#-*-coding:utf-8-*- import json import time from file.file import * def setReportDir(): #配置检测报告输出路径 header = [u'num1',u'num2',u'num3',u'num4',U'num5',u'状态'] curTime=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) reportName=U'检测报告'+curTime+'.xlsx' outReport='D:/' outReportDir=outReport+reportName report = Exp2xlsx(header,outReportDir) return report if __name__ == "__main__": #配置检测报告内容及路径 report=setReportDir() #读取json格式文件 jsonText=u'' with open("aaa.txt",'r') as jsonText: jsonReadText=json.load(jsonText) status=jsonReadText["status"] objData=jsonReadText["objData"] batchId= objData["batchNo"] detectionResult=objData["detectionResult"] for result in detectionResult: indexNo=result["indexNo"] result=result["result"] if len(result)>0: for resultNo in result: detectionMode=resultNo["detectionMode"] detectionResult=resultNo["detectionResult"] detectionInfo=resultNo["detectionInfo"] #保存检测报告为excel dataList = [batchId, indexNo, detectionMode,detectionResult,detectionInfo,status] report.save(dataList) else: detectionMode=u'无结果' detectionResult=u'无结果' detectionInfo=u'无结果' #保存检测报告为excel dataList = [batchId, indexNo, detectionMode,detectionResult,detectionInfo,status] report.save(dataList)