Ok, so I decided to start learning Python. I'm a net admin by trade, with some C/C++ classes years ago. I've started reading Learn Python the Hard Way and I'm over halfway through the book, but I decided to take a break and start working on a little project. So far I've overcome some little issues and moved forward, but now I'm stuck and I'm not seeing the way around.
In Python I get my prompt, then I enter the XML file, then the error "global name 'element' not defined". I'm missing the way to pull variables between functions, and element isn't a simple str or int I can just define "global element" somewhere. I'm probably over my head in coding so far, but the rest of the book work appears to be socket/game coding and I'm looking to build a simple note taking app for now.
Code:
# Import helper functions and code
from xml.etree.ElementTree import ElementTree
from xml.parsers.expat import ExpatError
# Define functions
def parse_xml(note_file):
""" parses the specified XML database """
results = []
print "ID | Title"
for element in note_file.getiterator('note'):
date = element.find('date')
results.append({
'id' : element.get('id'),
'title' : element.find('title').text,
'body' : element.find('body').text,
'date' : {
'day' : date.find('day').text,
'month' : date.find('month').text,
'year' : date.find('year').text,
},
})
def open_xml(tree):
""" open the XML database, pass it to the parse_xml function """
try:
note_file = ElementTree(file = tree)
except ExpatData:
print "Unable to parse XML from file"
parse_xml(note_file)
print element.get('id'), "|", element.find('title').text
noteDB = raw_input('What file would you like to open? ')
open_xml(noteDB)
In Python I get my prompt, then I enter the XML file, then the error "global name 'element' not defined". I'm missing the way to pull variables between functions, and element isn't a simple str or int I can just define "global element" somewhere. I'm probably over my head in coding so far, but the rest of the book work appears to be socket/game coding and I'm looking to build a simple note taking app for now.