Re: How do I replace multiple elements in a document?(using ElementTree)

Tony McDonald <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
On 30 Nov 2005, at 16:27, Fredrik Lundh wrote:

> Tony McDonald wrote:
>
>> I did have this,
>>
>> from elementtree.ElementTree import XML, ElementTree, Element,
>> SubElement, dump
>>
>> and went to this;
>>
>> from cElementTree import XML, ElementTree, Element, SubElement, dump
>>
>> Which imports fine, but there's something amiss because;
>> root = ElementTree(file=filename)
>> ...
>> elements = root.getiterator()
>> print "LEN", len(elements)
>>
>> which gave 634 elements using python ElementTree but gives len() of
>> unsized object when using the eElementTree version, and there does
>> seem to be something amiss ...
>
> the getiterator method is defined to return "something that you can
> iterate over", which is not necessarily a sequence object.
>
> in ElementTree, it happens to be a list (at least in the 1.2  
> series), but
> cElementTree returns a generator instead.
>
> no matter what version you're using, this
>
>     for elem in root.getiterator():
>         ...
>
> is guaranteed to work.

and indeed it does. Rapidly!


> if you want a list, you can always use the list
> constructor. e.g.
>
>     print "LEN", len(list(root.getiterator()))
>
> </F>
>

Many thanks Fredrik, this thread has helped me out no end!

cheers
Tone
-- 
Dr Tony McDonald, Asst Director, FMSC. 0191 246 4543
School of Medical Education Development
Project Manager, FDTL-4 ePortfolios http://www.eportfolios.ac.uk/

_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.