By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,788 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,788 IT Pros & Developers. It's quick & easy.

Re: elementtree and rounding questions

P: n/a
jy******@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:

tree.findall("//template//fontData")

("//" 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".

Stefan
Jul 30 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.