473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: elementtree and rounding questions

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
0 852

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
1
by: mirandacascade | last post by:
I do not understand how to use the find() method in ElementTree. The file 'sample.xml' is: <?xml version="1.0"?> <SampleRoot> <Header> <Product>FindMystery</Product> </Header>...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
7
by: mirandacascade | last post by:
O/S: Windows XP Home Vsn of Python: 2.4 Copy/paste of interactive window is immediately below; the text/questions toward the bottom of this post will refer to the content of the copy/paste ...
0
by: Gabriel Genellina | last post by:
En Wed, 30 Jul 2008 00:56:55 -0300, <jyoung79@kc.rr.comescribi�: If all you need is to *display* the value - use formatting expressions like %f, the locale variants, the Template class, or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.