python3 xml_to_dict、dict_to_xml等互相转换的方法
from basic_config_vars.config_vars import moviesAllFilePath,moviesFilePath #自定义的一些文件变量
import xmltodict
import json
import os
import traceback class XmlDict():
def __init__(self):
pass #初始化数据,传入参数1、xml文件路径 2、xml格式字符串 3、json字符串 4、dict字典
def init_data(self,file_Str_Dict):
if isinstance(file_Str_Dict,str) and os.path.isfile(file_Str_Dict):
with open(file_Str_Dict) as fp:
self.data=fp.read()
elif isinstance(file_Str_Dict,(str,dict)):
self.data=file_Str_Dict def xml_to_dict(self,file_Str_Dict):
self.init_data(file_Str_Dict) data_orderedD=xmltodict.parse(self.data)
data_json=json.dumps(data_orderedD,indent=4)
data_dict=json.loads(data_json) return data_dict def dicttoxml(self,file_Str_Dict):
self.init_data(file_Str_Dict)
if isinstance(self.data,str):
try:
self.data=json.loads(self.data)
except:
print('传入参数非json或dict类型,无法转换。')
traceback.print_exc()
return None
return xmltodict.unparse(self.data,pretty=True,encoding='gb2312') if __name__=='__main__':
#obj=XmlDict(moviesFilePath) obj = XmlDict()
d=obj.xml_to_dict(moviesAllFilePath)
print(d)
print('*'*30)
#print(obj.dicttoxml(json.dumps(d)))
print(obj.dicttoxml('{"nihao":{"ty":"leixing","fuoran":"feichang"}}'))
print('hahaha')
XMl文件参考:
<collection shelf="New Arrivals">
<movie title="Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year>1989</year>
<rating>R</rating>
<stars>8</stars>
<description>A schientific fiction</description>
</movie>
<movie title="Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes>4</episodes>
<rating>PG</rating>
<stars>10</stars>
<description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars>2</stars>
<description>Viewable boredom</description>
</movie>
</collection>
python3 xml_to_dict、dict_to_xml等互相转换的方法的更多相关文章
- 微信录音文件上传到服务器以及amr转化成MP3格式,linux上转换简单方法
微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...
- C#中二进制、十进制和十六进制互相转换的方法
二进制在C#中无法直接表示,我们一般用0和1的字符串来表示一个数的二进制形式.比如4的二进制为"100".下面介绍C#里面用于进制转换的方法. 十进制转换为二进制(int--> ...
- jquery对象与dom对象之间互相转换的方法
本文主要讲述jquery对象和js里的dom对象之间互相转换的方法,使jquery对象可以直接使用js里的方法,或js里的dom对象使用jquery里的方法. jquery对象和dom对象是不一样的, ...
- 托管和非托管转换新方法:Marshaling Library(zz) 【转】
托管和非托管转换新方法:Marshaling Library(zz) 托管和非托管转换新方法:Marshaling Library(zz) http://hi.baidu.com/superql/bl ...
- 【ABAP系列】SAP ABAP 行列转换的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...
- 【转】在Python的struct模块中进行数据格式转换的方法
这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...
- python3.6中 字典类型和字符串类型互相转换的方法
mydic = {"俄罗斯": {"1":"圣彼得堡", "2":"叶卡捷琳堡", "3& ...
- python3 中encode 和decode的使用方法。
编码: 将文本转换成字节流的过程.即Unicode----------->特定格式的编码方式,产生特定的字节流保存在硬盘中(一般为utf-8格式). 解码: 将硬盘中的字节流转换成文本的过程.即 ...
- 使用js进行string和json之间转换的方法
在数据传输过种中,json是以文本,即字符串的形式传递,字符串形似Json对象: var str1 = '{ "name": "Amy", "sex& ...
随机推荐
- js set集合转数组 Array.from的使用方法
1.set集合转化Array数组 注意:这个可以使用过滤数组中的重复的元素 你可以先把数组转化为set集合 然后在把这个集合通过Array.from这个方法把集合在转化为数组 var set = n ...
- 最新 浩德钢圈java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.浩德钢圈等10家互联网公司的校招Offer,因为某些自身原因最终选择了浩德钢圈.6.7月主要是做系统复习.项目复盘.Leet ...
- Oracle ROWNUM的陷阱
先抛出一个问题: 我有一张表T,现在我想对表中1/4的记录作UPDATE操作,我的SQL如下: Update t set col1='123' where mod(rownum,4)=1 我能够得到想 ...
- liunx mysql数据库目录迁移
1.查看mysql安装目录 从目录etc/my.cnf中查看安装目录 2.进入mysql目录,停止mysql服务 命令: cd usr/local/mysql 命令:service mysql sto ...
- 嵌入式02 STM32 实验02 端口输入输出各4种模式
GPIO(General-purpose input/output 通用目的输入/输出端口) 电压(A模拟量)与电平(D数字量) GPIO 8种工作模式(输入四种.输出四种) 1.GPIO_Mode_ ...
- int main(int argc, char *argv[])解释
int main(int argc, char *argv[]) 详解: #include <stdio.h> int main(int argc, char *argv[]) { int ...
- go build、go install、go get命令详解
(内容凌乱,日后整理!) 原文链接:https://blog.csdn.net/zhangliangzi/article/details/77914943 GO下载: GO语言中文网下载:https: ...
- U盘改造计划之PE、kali、U盘三合一
最强U盘攻略之一 前一段时间朋友买电脑问了我一些问题,我突然发现U盘怎么这么便宜,128G金士顿,140?!!!我16年买的可是240啊.买贵一百块,我好方啊.但是我的U盘,我是不会屈服做一个普通的U ...
- 缺陷的背后(四)---多进程之for循环下fork子进程引发bug
导语 业务模块为实现高并发时的更快的处理速度,经常会采用多进程的方式去处理业务.多进程模式下常见的三种bug:for循环下fork子进程导致产生无数孙子进程,僵尸进程,接口窜包.本章主要介绍第一种常见 ...
- jquery easyui datagrid的一些用法
获取选中的多选数据 var rows = $('#Id').datagri('getSelections'); 选中单行的数据 var row = $(#Id).datagrid('getSelect ...