elementtree and rounding questions

Stefan Behnel stefan_ml at behnel.de
Wed Jul 30 08:25:53 CEST 2008

jyoung79 at kc.rr.com wrote:
> ============================================================
> <data>
> 	<fonts>
> 		<fontData embed="true" name="Times" />
> 		<fontData embed="true" name="Arial" />
> 	</fonts>
> 	<color>
> 	</color>
> 	<template>
> 		<fonts>
> 			<fontData>
> 				<fontData embed="true" name="Courier">text</fontData>
> 			</fontData>
> 			<fontData embed="true" name="Helvetica" />
> 		</fonts>
> 		<width>
> 		</width>
> 	</template>
> </data>
> ============================================================

You can exploit the structure: the Elements you are looking for do not have
children, but they do have a "name" attribute.

  declarations = [ fontData for fontData in root.getiterator('fontData')
                   if fontData.get("name") ]

This gives you a list of all "fontData" Elements that declare a font name. The
one you want is the last one, i.e. declarations[-1].

You can also do

  font_names = [ fontData.get("name")
                 for fontData in root.getiterator('fontData')
                 if fontData.get("name") ]

or something like that. And if you only want the fontData Elements that appear
in the template Elements, try findall instead of getiterator:


("//" means: descend into the subtree, while "/" would mean: look only at the
direct children).

> f = root.getiterator('fonts')
> n = f[-1].getiterator('fontData')

You should not rely on ET returning a list from ".getiterator()", so avoid
using f[-1] here.

> fntList = []
> f = root.getiterator('fonts')
> n = f[-1].find('fontData')
> for i in n:
>     fntList.append(i.get('name'))
> print fntList
> ============================================================
> This code just gave me ['Courier'].  Now if I change 'find' to 'findall' then I'll get [None, 'Helvetica'].  Not exactly sure what exactly that's doing.

The first value (None) comes from the "fontData" Element that does not have a
"name" attibute. The path expression "fontData" means: find all children that
are named "fontData". As above, what you want is ".//fontData".


More information about the Python-list mailing list