Files
Python_CookBook_repo/6.数据编码与处理/5.将字典转换为XML.py
2025-09-10 16:12:45 +08:00

22 lines
436 B
Python

from xml.etree.ElementTree import Element
def dict_to_xml(tag, d):
# 创建一个element实例
elem = Element(tag)
# 将字典键值对拆开填入
for k, v in d.items():
child = Element(k)
child.text = str(v)
elem.append(child)
return elem
if __name__ == '__main__':
s = {"name": 'GOOG',
"shares": 100,
"price": 490.1}
e = dict_to_xml("stock", s)
print(e)