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

anyone?

You changed the indentation of for subelement in element: .

??? that should make a difference

Yes because with your new code, the for loop is executed only if the element has a text, which is not what you want

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 done that and still the sodding same

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.