Python 提取dict转换为xml/json/table并输出的实现代码
作者:mdxy-dxy 时间:2021-01-26 17:56:17
核心代码:
#!/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)
标签:Python,提取dict
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python opencv实现图像边缘检测
2022-08-05 08:40:34
![](https://img.aspxhome.com/file/2023/4/86404_0s.jpg)
Python使用sftp实现传文件夹和文件
2021-09-30 12:27:37
![](https://img.aspxhome.com/file/2023/1/76881_0s.jpg)
python变量赋值方法(可变与不可变)
2021-02-14 08:51:55
![](https://img.aspxhome.com/file/2023/9/69559_0s.jpg)
python 实现朴素贝叶斯算法的示例
2023-12-09 12:59:11
ASP利用TCPIP.DNS组件实现域名IP查询
2010-02-26 11:25:00
HTML文件HEAD内部标签用法浅析
2008-07-06 20:56:00
聊聊python中令人迷惑的duplicated和drop_duplicates()用法
2022-01-03 19:10:57
![](https://img.aspxhome.com/file/2023/4/87584_0s.png)
Python 操作 PostgreSQL 数据库示例【连接、增删改查等】
2021-12-14 00:54:08
利用Python判断你的密码难度等级
2021-04-10 20:46:08
说说CSS Hack 和向后兼容
2010-05-17 13:11:00
![](https://img.aspxhome.com/file/UploadPic/20105/17/css-hack-and-standard-31s.png)
最新PyCharm从安装到PyCharm永久激活再到PyCharm官方中文汉化详细教程
2022-09-24 01:38:47
![](https://img.aspxhome.com/file/2023/0/82940_0s.png)
Python语言实现二分法查找
2021-12-01 18:39:49
![](https://img.aspxhome.com/file/2023/5/85375_0s.png)
Python datatime库语法使用详解
2022-01-30 03:16:17
![](https://img.aspxhome.com/file/2023/6/83846_0s.png)
python实现数通设备tftp备份配置文件示例
2022-12-02 13:59:21
CSS hacks与争议
2007-11-19 12:56:00
![](https://img.aspxhome.com/file/UploadPic/200711/19/20071119125952144s.gif)
基于insertBefore制作简单的循环插空效果
2023-08-17 22:34:17
![](https://img.aspxhome.com/file/2023/2/55932_0s.png)
python重试装饰器示例
2022-07-28 10:03:12
爱你就要说出来,来表白吧
2008-11-23 16:23:00
![](https://img.aspxhome.com/file/UploadPic/200811/23/loveyou-38s.gif)
Python 正则表达式入门(初级篇)
2021-12-03 23:41:12
安装了Office2003补丁之后,access不能用,打不开了
2011-05-12 12:19:00