Python中xml和dict格式转换的示例代码
作者:飓风肆虐的青春 时间:2021-07-18 21:30:10
在做接口自动化的时候,请求数据之前都是JSON格式的,Python有自带的包来解决。最近在做APP的接口,遇到XML格式的请求数据,费了很大劲来解决,解决方式是:接口文档拿到的是XML,在线转化为json格式(目的是拿到xml数据的模板),存放到json文件中,根据接口名去提取。
github原文介绍:使用XML的Python模块感觉就像您在使用JSON
链接:https://github.com/martinblech/xmltodict
下载xmltodict(pip install xmltodict)
C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pip install xmltodict
Requirement already satisfied: xmltodict in c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site-packages
(0.12.0)
直接演示了,准备一个原始的XML的数据
<?xml version="1.0"?>
<mydocument has="an attribute">#has是节点属性
<and>
<many>elements</many>
<many>more elements</many>
</and>
<plus a="complex">
element as well
</plus>
</mydocument>
由于目前没有找到能够将XML直接赋值给函数的方法,所以先用在线转化工具将XML转化为JSON,转化后的结果为
{
"mydocument": {
"-has": "an attribute", #在这里要留意一个事情,XML是有节点的,节点有属性,json数据的键之前有-的,就是属性,但是xmltodict识别的属性是@,所以要把-替换为@(就是这里找了好久)
"and": {
"many": [
"elements",
"more elements"
]
},
"plus": {
"-a": "complex",
"#text": "
element as well
"
}
}
}
示例代码
dictdata={
"mydocument": {
"@has": "an attribute",
"and": {
"many": [
"elements",
"more elements"
]
},
"plus": {
"@a": "complex",
"#text": "element as well"
}
}
}
xmldata=xmltodict.unparse(dictdata,pretty=True)#dict转xml
print(xmldata)#解析结果可以复制运行查看,结果是原始数据
new_dictdata=xmltodict.parse(xmldata,process_namespaces = True)#xml转dict
print(new_dictdata)#在这里有一个注意事项,new_dictdata的数据格式是<class 'collections.OrderedDict'>,并不直接是dict,需要自行处理
"""
处理方式如下
"""
key_dictdata=dict(new_dictdata)
value_dictdata=dict(dict(new_dictdata)["mydocument"])
key_dictdata["mydocument"]=value_dictdata
print(key_dictdata)
上面就是xml和dict转化,如果需要转化json,内置的json模块就可以完成,但是在自动化测试框架中这样使用比较麻烦,而且复用性不好,封装好如下
#-*- coding: utf-8 -*
#@author 小测试
#@create 2019-10-30 15:46
import xmltodict
"""
xml和dict转换
"""
def dict_xml(dictdata):
"""
dict转xml
dictstr: dict字符串
return: xml字符串
"""
xmlstr=xmltodict.unparse(dictdata, pretty=True)
return xmlstr
def xml_dict(xmldata,moudle):
"""
xml转dict
xmlstr: xml字符串
moudle:根节点
return: dict字符串
"""
data=xmltodict.parse(xmldata,process_namespaces = True)
dictdata=dict(data)
_dictdata=dict(dictdata[moudle])
dictdata[moudle]=_dictdata
return dictdata
总结
以上所述是小编给大家介绍的Python中xml和dict格式转换的示例代码网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/Testking/p/11809532.html
标签:Python,xml,dict,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python脚本Selenium及页面Web元素定位详解
2023-06-14 12:21:10
![](https://img.aspxhome.com/file/2023/2/97742_0s.png)
pycharm2022没有manage repositories配置镜像源的解决方法
2022-06-27 05:30:24
![](https://img.aspxhome.com/file/2023/9/78449_0s.png)
Pytorch中retain_graph的坑及解决
2022-12-20 16:21:09
Python自动化办公之Excel拆分与自动发邮件
2022-05-03 09:05:40
![](https://img.aspxhome.com/file/2023/4/93464_0s.png)
python中的class_static的@classmethod的巧妙用法
2022-07-18 15:54:43
SQL Server小知识:Processor Affinity
2008-11-24 20:50:00
css设计小巧三条
2008-01-21 13:04:00
Python抓取京东图书评论数据
2023-10-16 09:26:37
将pandas.dataframe的数据写入到文件中的方法
2022-07-13 14:56:38
![](https://img.aspxhome.com/file/2023/1/77791_0s.jpg)
Python read函数按字节(字符)读取文件的实现
2021-08-20 09:40:48
php 模拟get_headers函数的代码示例
2023-09-09 06:16:36
Python 自动化修改word的案例
2021-11-08 21:18:16
![](https://img.aspxhome.com/file/2023/7/64357_0s.jpg)
如何使用Django Admin管理后台导入CSV
2022-12-28 19:56:52
![](https://img.aspxhome.com/file/2023/5/69635_0s.png)
python函数和python匿名函数lambda详解
2021-08-22 17:57:07
详细解读Python的web.py框架下的application.py模块
2021-06-24 22:28:47
最新idea2021最新激活超详细教程
2023-08-08 01:50:17
![](https://img.aspxhome.com/file/2023/8/63438_0s.png)
Python包管理工具pip的15 个使用小技巧
2023-08-30 17:30:51
![](https://img.aspxhome.com/file/2023/4/62594_0s.png)
Python函数定义及传参方式详解(4种)
2022-08-16 14:39:08
ASP隐藏真实文件的下载功能实现代码
2011-04-14 11:12:00
在Dreamweaver MX中应用“占位图形”
2009-07-10 13:16:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113314790s.gif)