I have this script and it was working a couple of weeks ago.

Now when i run it nothing is printed out other than white space. WHY???

#!/usr/bin/env python

from xml.etree import ElementTree as ET
import os
import urllib

def find_text(element):
        if element.text is None:
                for subelement in element:
                        for txt in find_text(subelement):
                                yield txt

        else:
                yield element.text

feed = urllib.urlopen("http://server-up.theatticnetwork.net/demo/")
try:
    tree = ET.parse(feed)
		
except Exception, inst:
    print "Unexpected error opening %s: %s" % (tree, inst)
    
root= tree.getroot()

for txt in find_text(root):
        print txt

Your implementation of find_text would only attempt to iterate to lower elements if the current element had no text.

Having text does not preclude having child nodes.

I rewrote your find_text as follows and I'm seeing output now:

def find_text(element):
        if element.text:
                yield element.text
                
        for subelement in element:
                for txt in find_text(subelement):
                        yield txt

that works, thanks

just one problem it will work in my python shell but it wont work in my cgi bin on my website what have i missed

#!/usr/bin/env python
import elementtree.ElementTree as ET
import os
import urllib
print "Content-type: text/html\n"

def find_text(element):
      if element.text:
          yield element
          for subelement in element:
              for txt in find_text(subelement):
                  yield txt

feed = urllib.urlopen("http://server-up.theatticnetwork.net/demo/")
try:
    tree = ET.parse(feed)
		
except Exception, inst:
    print "Unexpected error opening %s: %s" % (tree, inst)
    
root= tree.getroot()
text = root.getchildren()

for item in text:
    if item.tag =="Memory":
        extra = "Memory"
    else:
        extra = "Other"

    for element in find_text(item):
        print extra+element.tag

You should rewrite find_text as

def find_text(element):
    if element.text:
        yield element
    for subelement in element:
        for txt in find_text(subelement):
            yield txt

If you wrote it like this, I can't see how it could fail.

i have contacted the hosting company and they said they got the following

=================================
Content-type: text/html

adam
Traceback (most recent call last):
File "adam.py", line 20, in ?
print "Unexpected error opening %s: %s" % (tree, inst)
NameError: name 'tree' is not defined
=================================

but i dont get that error. What is causing this problem?

It was the hosting company change the version of element tree module so i had to change the import to import cElementTree as ET

Thanks for all the help

Adam

This question has already been answered. Start a new discussion instead.