Skip to content

Instantly share code, notes, and snippets.

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

  • Save walkerdb/1ed64be7d0abeff2a6e1 to your computer and use it in GitHub Desktop.

Select an option

Save walkerdb/1ed64be7d0abeff2a6e1 to your computer and use it in GitHub Desktop.
# grab a single extent. Remember xpath returns a list, so we have to specify an individual element
>>> extent = tree.xpath("//extent")[0]
# now we have an "element" object, in this case an extent tag
# we can access all sorts of stuff from here.
# to get the text contained in the tag:
>>> extent.text
'3 linear feet and 1 outsize box'
# to get the tag type:
>>> extent.tag
'extent'
# to get a dictionary containing all the attributes:
>>> extent.attrib
{'encodinganalog': '300'}
# to get just one attribute from the extent
>>> extent.attrib["encodinganalog"]
'300'
# the previous example will give an error if the tag doesn't have the specified attribute
# for a more resilient option, use the dictionary .get method instead
>>> extent.attrib.get("encodinganalog", "")
'300'
# to get the parent element of the extent:
>>> parent = extent.getparent()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment