40 - xml文档与字典之间的互相转换
【摘要】 1. 如何将一个字典转换为xml文档,并将该xml文档保存成文本文件
'''
dicttoxml
pip install dicttixml
'''
import dicttoxml
from xml.dom.minidom import parseString
d = [20, 'names', {'name': 'Bill', 'age': '30', 'sa...
1. 如何将一个字典转换为xml文档,并将该xml文档保存成文本文件
'''
dicttoxml
pip install dicttixml
'''
import dicttoxml
from xml.dom.minidom import parseString
d = [20, 'names', {'name': 'Bill', 'age': '30', 'salary': 2000}, {'name': 'Mike', 'age': '20', 'salary': 3000}, {'name': 'John', 'age': '40', 'salary': 4000}]
bxml = dicttoxml.dicttoxml(d, custom_root='persons')
xml = bxml.decode('utf-8')
print(xml)
dom = parseString(xml)
prettyxml = dom.toprettyxml(indent=' ')
print(prettyxml)
f = open('persons1.xml', 'w', encoding='utf-8')
f.write(prettyxml)
f.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
<?xml version="1.0" encoding="UTF-8" ?><persons><item type="int">20</item><item type="str">names</item><item type="dict"><name type="str">Bill</name><age type="str">30</age><salary type="int">2000</salary></item><item type="dict"><name type="str">Mike</name><age type="str">20</age><salary type="int">3000</salary></item><item type="dict"><name type="str">John</name><age type="str">40</age><salary type="int">4000</salary></item></persons>
<?xml version="1.0" ?>
<persons> <item type="int">20</item> <item type="str">names</item> <item type="dict"> <name type="str">Bill</name> <age type="str">30</age> <salary type="int">2000</salary> </item> <item type="dict"> <name type="str">Mike</name> <age type="str">20</age> <salary type="int">3000</salary> </item> <item type="dict"> <name type="str">John</name> <age type="str">40</age> <salary type="int">4000</salary> </item>
</persons>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
<persons>
<item type="int">20</item>
<item type="str">names</item>
<item type="dict">
<name type="str">Bill</name>
<age type="str">30</age>
<salary type="int">2000</salary>
</item>
<item type="dict">
<name type="str">Mike</name>
<age type="str">20</age>
<salary type="int">3000</salary>
</item>
<item type="dict">
<name type="str">John</name>
<age type="str">40</age>
<salary type="int">4000</salary>
</item>
</persons>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2. 如何读取xml文档的内容,并将其转换为字典
'''
xmltodict
pip install xmltodict
'''
import xmltodict
f = open('products.xml', 'rt', encoding='utf-8')
xml = f.read()
import pprint
d = xmltodict.parse(xml)
print(d)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(d)
print(type(d))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
OrderedDict([('root', OrderedDict([('products', OrderedDict([('product', [OrderedDict([('@uuid', '1234'), ('id', '10000'), ('name', 'iphone9'), ('price', '9999')]), OrderedDict([('@uuid', '4321'), ('id', '20000'), ('name', '特斯拉'), ('price', '800000')]), OrderedDict([('@uuid', '5678'), ('id', '30000'), ('name', 'Mac Pro'), ('price', '40000')])])]))]))])
OrderedDict([ ( 'root', OrderedDict([ ( 'products', OrderedDict([ ( 'product', [ OrderedDict([ ( '@uuid', '1234'), ( 'id', '10000'), ( 'name', 'iphone9'), ( 'price', '9999')]), OrderedDict([ ( '@uuid', '4321'), ( 'id', '20000'), ( 'name', '特斯拉'), ( 'price', '800000')]), OrderedDict([ ( '@uuid', '5678'), ( 'id', '30000'), ( 'name', 'Mac ' 'Pro'), ( 'price', '40000')])])]))]))])
<class 'collections.OrderedDict'>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/104613601
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)