Форум сайта python.su
1
Есть xml файл с содержимым
<root> <child1/> <child2/> <child3/> </root>
Офлайн
857
>>> import lxml.html >>> >>> s = """ ... <root> ... <child1/> ... <child2/> ... <child3/> ... </root> ... """ >>> >>> d = {'child1': 'text1', ... 'child2': 'text2', ... 'child3': 'text3',} >>> >>> doc = lxml.html.fromstring(s) >>> for node in doc: ... node.text = d[node.tag] ... >>> out = lxml.html.tostring(doc, encoding='unicode') >>> print(out, end='') <root> <child1>text1</child1> <child2>text2</child2> <child3>text3</child3> </root> >>>
Офлайн