22 lines
436 B
Python
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)
|