Python extracts dict conversion to xml/json/table and outputs the implementation code _python

Source: Internet
Author: User

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)

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.