Skip to content

Instantly share code, notes, and snippets.

@walkerdb
Last active October 2, 2015 20:43
Show Gist options
  • Select an option

  • Save walkerdb/72652ccf1ccaa5332a2f to your computer and use it in GitHub Desktop.

Select an option

Save walkerdb/72652ccf1ccaa5332a2f to your computer and use it in GitHub Desktop.
# say we have a c01 element that looks like this:
'''
<c01>
[...skipping <did> tag for brevity]
<c02>
...
<note>This collection is haunted</note>
</c02>
</c01>
'''
# if we wanted to move the note up to the parent c0x-level element,
# this is how we'd do it
# grab the note element
# (we're assuming we already have the c01 element assigned to a variable "c01")
>>> note = c01.xpath("//note")[0]
# just appending the note to the end of the c01 element will do the trick:
>>> c01.append(note)
>>> print(etree.tostring(c01))
'''
<c01>
...
<c02>
...
</c02>
<note>This collection is haunted</note>
</c01>
'''
# if you want more location options than just the end of the element,
# use insert() with the index of the location you want to move things to:
>>> c01.insert(0, note)
>>> print(etree.tostring(c01))
'''
<c01>
<note>This collection is haunted</note>
...
<c02>
...
</c02>
</c01>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment