python dicttoxml模块简介

dicttoxml模块简介

官方文档

安装
pip install dicttoxml
基本用法
# 方法一 导入库
import dicttoxml
xml = dicttoxml.dicttoxml(some_dict)
# 方法二 导入dicttoxml()函数
form dicttoxml import dicttoxml
xml = dicttoxml(some_dict)
dicttoxml 属性介绍
root = False
创建一个xml片段,而不是完整的xml文档,默认True
>>> xml_snippet = dicttoxml.dicttoxml(obj, root=False)
>>> print(xml_snippet)
<mylist><item type="str">foo</item><item type="str">bar</item><item type="str">baz</item></mylist><mydict><foo type="str">bar</foo><baz type="int">1</baz></mydict><ok type="bool">true</ok>
custom_root=”根元素名称“(1.5版本开始)
自定义根,默认root
>>> xml = dicttoxml.dicttoxml(obj, custom_root='some_custom_root')
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><some_custom_root><mydict><foo>bar</foo><baz>1</baz></mydict><mylist><item>foo</item><item>bar</item><item>baz</item></mylist><ok>true</ok></some_custom_root>
xml_declaration = False(1.7.15版本开始)
省略xml声明()

虽然省略,但隐含默认编码信息仍然是:UTF-8

>>> xml = dicttoxml.dicttoxml(xml_declaration=False)
>>> print(xml)
<root><ok type="bool">true</ok><mylist type="list"><item type="str">foo</item><item type="str">bar</item><item type="str">baz</item></mylist><mydict type="dict"><foo type="str">bar</foo><baz type="int">1</baz></mydict></root>
attr_type = False (1.4版本开始)
禁用类型属性,默认True
>>> xml = dicttoxml.dicttoxml(obj, attr_type=False)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict><foo>bar</foo><baz>1</baz></mydict><mylist><item>foo</item><item>bar</item><item>baz</item></mylist><ok>true</ok></root>
encoding=“编码格式”(1.7.6版本开始)
更改XML编码属性,不改默认为:UTF-8
>>> xml = dicttoxml.dicttoxml(obj, encoding="ISO-8859-1")
include_encoding=False(1.7.6版本开始)
完全抑制编码属性,不包含编码属性,也没有隐含的默认编码属性,默认True
>>> xml = dicttoxml.dicttoxml(obj, include_encoding=False)
ids=True(1.1版本)

为每个元素提供一个唯一的id属性,默认False

>>> xml_with_ids = dicttoxml.dicttoxml(obj, ids=True)
>>> print(parseString(xml_with_ids).toprettyxml())
<?xml version="1.0" ?>
<root>
        <mylist id="root_160980" type="list">
                <item id="mylist_609405_1" type="str">foo</item>
                <item id="mylist_609405_2" type="str">bar</item>
                <item id="mylist_609405_3" type="str">baz</item>
        </mylist>
        <mydict id="root_140407" type="dict">
                <foo id="mydict_260437" type="str">bar</foo>
                <baz id="mydict_111194" type="int">1</baz>
        </mydict>
        <ok id="root_612831" type="bool">true</ok>
</root>
从1.3版本开始,dicttoxml接受从dict基类派生的类似dict的对象,并将其视为dict。例如:
>>> import collections
>>> dictlike = collections.OrderedDict({'foo': 1, 'bar': 2, 'baz': 3})
>>> xml = dicttoxml.dicttoxml(dictlike)
>>> print(xml)
312
同样从1.3版本开始,dicttoxml接受可迭代对象,并将其视为列表。例如:
>>> myiter = range(1,11)
>>> xml = dicttoxml.dicttoxml(myiter)
>>> print(xml)
12345678910
item_func=my_item_func(1.7版本开始)

不希望列表中项目元素的名称为item,可自定义名称,默认 item

>>> import dicttoxml
>>> obj = {u'mylist': [u'foo', u'bar', u'baz'], u'mydict': {u'foo': u'bar', u'baz': 1}, u'ok': True}
>>> my_item_func = lambda x: 'list_item'
>>> xml = dicttoxml.dicttoxml(obj, item_func=my_item_func)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict type="dict"><foo type="str">bar</foo><baz type="int">1</baz></mydict><mylist type="list"><list_item type="str">foo</list_item><list_item type="str">bar</list_item><list_item type="str">baz</list_item></mylist><ok type="bool">True</ok></root>
cdata=True(1.7.1版本开始)
cdata参数设置为True在CDATA中包装值。
>>> import dicttoxml
>>> obj = {u'mylist': [u'foo', u'bar', u'baz'], u'mydict': {u'foo': u'bar', u'baz': 1}, u'ok': True}
>>> xml = dicttoxml.dicttoxml(obj, cdata=True)
>>> print(xml)
<?xml version="1.0" encoding="UTF-8" ?><root><mydict type="dict"><foo type="str"><![CDATA[bar]]></foo><baz type="int"><![CDATA[1]]></baz></mydict><mylist type="list"><item type="str"><![CDATA[foo]]></item><item type="str"><![CDATA[bar]]></item><item type="str"><![CDATA[baz]]></item></mylist><ok type="bool"><![CDATA[True]]></ok></root>
return_bytes=False(1.7.14版本开始)
True:返回bytes对象(默认),False:返回str对象
>>> xml = dicttoxml.dicttoxml(obj)
>>> type(xml).__name__
'bytes'
>>> xml = dicttoxml.dicttoxml(obj, return_bytes=False)
>>> type(xml).__name__
'str'
拓展:
格式化打印,结合xml.dom.minidom模块实现格式化打印
>>> from xml.dom.minidom import parseString
>>> dom = parseString(xml)
>>> print(dom.toprettyxml())


    
        foo
        bar
        baz
    
    
        bar
        1
    
    true

调试
使用set_debug方法启用调试信息
默认情况下,调试信息记录到dicttoxml.log
>>> import dicttoxml
>>> dicttoxml.set_debug(debug=True)
Debug mode is on. Events are logged at: dicttoxml.log
>>> xml = dicttoxml.dicttoxml(some_dict)
更改调试信息记录路径
>>> dicttoxml.set_debug(debug=True, filename='/path/to/some_other_filename.log')
Debug mode is on. Events are logged at: some_other_filename.log
要关闭调试模式,只需使用False的参数调用set_debug
>>> dicttoxml.set_debug(debug=False)

你可能感兴趣的:(python,python)