Reputation: 1059
I have some xml with the element:
<seg id="1" text="some text"/>
which I want to reformat in python3 as:
<in_seg id="sent1"> some text</in_seg>
how do I do this?
Upvotes: 1
Views: 43
Reputation: 474021
You can create an element via instantiating an Element
class:
from lxml.etree import fromstring, Element, tostring
data = """
<seg id="1" text="some text"/>
"""
element = fromstring(data)
tag_name = 'in_' + element.tag
tag_id = 'sent' + element.attrib['id']
tag_text = element.attrib['text']
new_element = Element(tag_name, attrib={'id': tag_id})
new_element.text = tag_text
print(tostring(new_element))
Prints:
<in_seg id="sent1">some text</in_seg>
Upvotes: 1