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等互相转换的方法的更多相关文章

  1. 微信录音文件上传到服务器以及amr转化成MP3格式,linux上转换简单方法

    微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...

  2. C#中二进制、十进制和十六进制互相转换的方法

    二进制在C#中无法直接表示,我们一般用0和1的字符串来表示一个数的二进制形式.比如4的二进制为"100".下面介绍C#里面用于进制转换的方法. 十进制转换为二进制(int--> ...

  3. jquery对象与dom对象之间互相转换的方法

    本文主要讲述jquery对象和js里的dom对象之间互相转换的方法,使jquery对象可以直接使用js里的方法,或js里的dom对象使用jquery里的方法. jquery对象和dom对象是不一样的, ...

  4. 托管和非托管转换新方法:Marshaling Library(zz) 【转】

    托管和非托管转换新方法:Marshaling Library(zz) 托管和非托管转换新方法:Marshaling Library(zz) http://hi.baidu.com/superql/bl ...

  5. 【ABAP系列】SAP ABAP 行列转换的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 行列转换的方法 ...

  6. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

  7. python3.6中 字典类型和字符串类型互相转换的方法

    mydic = {"俄罗斯": {"1":"圣彼得堡", "2":"叶卡捷琳堡", "3& ...

  8. python3 中encode 和decode的使用方法。

    编码: 将文本转换成字节流的过程.即Unicode----------->特定格式的编码方式,产生特定的字节流保存在硬盘中(一般为utf-8格式). 解码: 将硬盘中的字节流转换成文本的过程.即 ...

  9. 使用js进行string和json之间转换的方法

    在数据传输过种中,json是以文本,即字符串的形式传递,字符串形似Json对象: var str1 = '{ "name": "Amy", "sex& ...

随机推荐

  1. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  2. SpringBoot系列教程JPA之delete使用姿势详解

    原文: 190702-SpringBoot系列教程JPA之delete使用姿势详解 常见db中的四个操作curd,前面的几篇博文分别介绍了insert,update,接下来我们看下delete的使用姿 ...

  3. Mybatis获取数据库自增主键

    一般我们都为将表中主键列设置为自增,当我们执行插入语句时,比如这样 //测试添加 Employee employee = new Employee(null, "jerry4",n ...

  4. ref,out,int参数复习

    ref 结构是值类型,按值传递.通过关键字ref,也可以通过引用传递结构. public static void ChangeA(ref A a) { a.X = ; } //如果A是结构类型,就添加 ...

  5. TCP/IP学习笔记2--协议分层与OSI参考模型

    协议的分层: 为解决不同计算机之间的通信,ISO(International Organization for Standards)指定了一个国际标准OSI(Open Systems Intercon ...

  6. 什么是渐进式Web App(PWA)?为什么值得关注?

    转载自:https://blog.csdn.net/mogoweb/article/details/79029651 在开始PWA这个话题之前,我们先来看看Internet现状. 截至2017年1月, ...

  7. [转帖]Linux教程(11)- linux中的计划作业

    Linux教程(11)- linux中的计划作业 2018-08-21 17:13:36 钱婷婷 阅读数 160更多 分类专栏: Linux教程与操作 Linux教程与使用   版权声明:本文为博主原 ...

  8. [Oracle] - 查看数据库中每个表占用空间大小,及进行表压缩

    查询用户创建的表 select * from user_tab_comments; -- 查询本用户的表,视图等. select * from user_col_comments; -- 查询本用户的 ...

  9. Serializer组件

    Serializer组件 一 .序列化器-Serializer 作用: 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串 反序列化,把客户端发送过来的数据,经过req ...

  10. gin - 读取Body后再次赋值

    这样就不影响后面读取了