I am trying to print out a dictionary result. The dictionary is made from a database search. I have tried to print it in every possible way

here is the code

#!/usr/bin/python
import elementtree.ElementTree as ET
import urllib
import MySQLdb
print "Content-type: text/html\n"
info={}
def find_text(element):
    if element.text is None:
        for subelement in element:
            for txt in find_text(subelement):
                yield txt
                
    else:
        yield element


# connect
db = MySQLdb.connect(host = "localhost", user = "username", passwd = "password", db = "adamplo1_UniProject")

# create a database cursor
cursor = db.cursor(MySQLdb.cursors.DictCursor)

# execute SQL select statement
cursor.execute("SELECT * FROM Threshold")

Results = cursor.fetchall()

    
    


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 element in text:
    if element.tag == "Memory":
        for element in find_text(element):
            print element.tag


print "<br />"
print Results[Free]
print Results['Free']
print Results["Free"]
print Results["'Free'"]

url = http://adamplowman.com/cgi-bin/getting_xml_info%20copy.py

What is the output of print(Results) ?

It looks like a tuple which first element is a dict. Try Results[0]["Free"]

ok that work well, but i am tring to print out the value from the database that corresponds to the name in the XML for example

XML tag name
Free
Used

Database
({'Used': 1L, 'BuffersPercent': 1L, 'Cached': 1L, 'App': 1L, 'Percent': 1L, 'Free': 1L, 'AppPercent': 1L, 'CachedPercent': 1L, 'Total': 1L, 'Buffers': 1L},)


so i print out the xml Free and the corresponding 1 from the database results.

root= tree.getroot()
text = root.getchildren()
for element in text:
    if element.tag == "Memory":
        for element in find_text(element):
            print element.tag
            print Results[0][element.tag]

but i get nothing

turns out there was a space in the element.tag. Once i remove that everything works ok.

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.