核心代码:
- #!/usr/bin/python
- #-*- coding:gbk -*-
- #设置源文件输出格式
- import sys
- import getopt
- import json
- import createDict
- import myConToXML
- import myConToTable
- def getRsDataToDict():
- #获取控制台中输入的参数,并根据参数找到源文件获取源数据
- csDict={}
- try:
- #通过getopt获取参数
- opts,args=getopt.getopt(sys.argv[1:],"",["output=","file=","query="])
- csDict=createDict.procParam(opts,args)
- return csDict
- except getopt.GetoptError:
- print getopt.error
- sys.exit()
- def collectionToJson(contentTxt):
- #参数1表示python对象;参数2表示按照字典排序;参数3表示根据格式缩进显示
- jsoninfo = json.dumps(contentTxt,sort_keys=True,indent=2)
- print "JSON输出:"
- print type(jsoninfo)
- print jsoninfo
- if __name__=="__main__":
- #输入参数格式为>python test.py output=json file=c:\..\input.txt query=Permission[0]
- inputParm=getRsDataToDict()
- if inputParm["query"]!=None:
- csDict=createDict.getQueryRs(inputParm["contentTxt"],inputParm["query"])
- else:
- csDict=inputParm["contentTxt"]
- output=inputParm["output"]
- if output=="json":
- collectionToJson(csDict)
- elif output=="xml":
- path='C:\\Users\\Vincent\\Documents\\MyTest1.xml'
- encod='utf8'
- myConToXML.getDictToXml(csDict,path,encod)
- elif output=="table":
- myConToTable.ContentToTable(csDict)