Python extracts dict and converts it to xml/json/table, and outputs the implementation code, dictjson
Core code:
#! /Usr/bin/python #-*-coding: gbk-*-# Set the source file output format import sysimport getoptimport jsonimport createDictimport myConToXMLimport myConToTable def getRsDataToDict (): # obtain the input parameters in the console and find the source data csDict ={} try: # Get the opts parameter through getopt, args = getopt. getopt (sys. argv [1:], "", ["output =", "file =", "query ="]) csDict = createDict. procParam (opts, args) return csDict failed t getopt. getoptError: print getopt. error sys. exit () def coll EctionToJson (contentTxt): # parameter 1 indicates a python object; parameter 2 indicates that the object is ordered alphabetically; parameter 3 indicates that jsoninfo = json is displayed according to the indent format. dumps (contentTxt, sort_keys = True, indent = 2) print "JSON output:" print type (jsoninfo) print jsoninfo if _ name __= = "_ main __": # The input parameter format is> 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)