Core code:
#!/usr/bin/python #-*-coding:gbk-*-#设置源文件输出格式 import sys import getopt import JSON import createdict import Mycontoxml Import mycontotable def getrsdatatodict (): #获取控制台中输入的参数, and based on parameters find source file get source data csdict={} try: #通过getopt获取参数 op
Ts,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对象; parameter 2 means sorted by dictionary; parameter 3 means root According to the format indent display jsoninfo = Json.dumps (contenttxt,sort_keys=true,indent=2) print "JSON output:" Print type (jsoninfo) print Jsoni NFO if __name__== "__main__": #输入参数格式为 >python test.py output=json file=c:\ ... \input.txt Query=permission[0] Inputparm=getrsdatatodict () if inputparm["Query"]!=none:csdict=createdict.getquery Rs (inputparm["contenttxt"],inputparm["Query"]) else:csdict=inputparm["Contenttxt"] output=inputparm["Output" I F output== "JSON": Collectiontojson (CSDict) elif output== "xml": Path= ' c:\\users\\vincent\\documents\\mytest1.xml ' encod= ' UTF8 ' oXML (csdict,path,encod) elif output== "table": Mycontotable.contenttotable (Csdict)