使用dicttoxml包将Python字典转换为xml表示形式。
首先,安装dicttoxml软件包pip3 install dicttoxml
创建一个字典对象>>> D1={"name":"Ravi", "age":21, "marks":55}
现在dicttoxml()从dicttoxml包中导入函数,并使用D1作为参数。该函数返回已编码的字符串作为字典的xml表示形式>>>fromdicttoxml import dicttoxml
>>>xml=dicttoxml(D1)
通过decode()函数获取解码字符串
结果字符串包含字典的xml版本>>>xml=xml.decode()
>>>xml
'<?xml version="1.0" encoding="UTF-8" ?>Ravi2155'
您甚至可以将其存储在xml文件中>>>xmlfile=open("dict.xml","w")
>>>xmlfile.write(xml.decode())
>>>xmlfile.close()