Python extracts dict and converts it to xml/json/table, and outputs the implementation code, dictjson

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.