In [1]:
# this block is just for the style sheet for the notebook
from IPython.core.display import HTML
def css_styling():
styles = open("styles/custom.css", "r").read()
return HTML(styles)
css_styling()
Out[1]:
In [6]:
# import the appropriate libraries
import xml.etree.ElementTree as ET # xml processing
In [8]:
# read the XML file
tree = ET.parse('scripts/menu.xml')
In [15]:
print 'tree element:\t', tree
In [16]:
# get the root of the tree
root = tree.getroot()
In [17]:
print 'root element:\t ', root
In [18]:
# make a list of all the <food> tags
food_tags = root.findall('food')
print 'number of food tags = ', len(food_tags)
In [20]:
# print the <food> tags - it's not what you would expect
print food_tags
In [26]:
# access the enties in the list
first_food_item = food_tags[0]
print 'the first child node is:\t', first_food_item
In [24]:
# here's how we can view a child node's content
ET.dump(first_food_item)
In [27]:
section = 'food'
tag = 'price'
node = root.find(section)
subnode = node.find(tag)
print "Path to Price subnode of Food node:"
print "Root:", str(root), " Node: ", node, "Subnode: ", subnode
print
In [28]:
#Specify the path to the 'name' attribute of the 'food' node
node = root.find(section)
attribute = node.attrib['name']
print "Path to Name attribute of Food node:"
print "Root:", str(root), " Node: ", node, "Attribute: ", attribute
print
In [29]:
#Find the attributes of each food node
print "All nodes, subnodes and attributes:"
for node in root:
print node.tag, node.attrib
for subnode in node:
print subnode.tag, subnode.text
print
In [31]:
#Add a new attribute to each food tag
for node in tree.iter(tag='food'):
node.set('category', 'breakfast')
In [30]:
# you can search by name
name = 'Belgian Waffles'
for selected_name in root.findall("./food/[@name='%s']" % name):
#print the description associated with the selected name
print "Found Belgian Waffles!"
print name, ":", selected_name.find('description').text
In [32]:
#find a specific node
#and update a subnode
for node in tree.iter(tag='food'):
if node.attrib['name'] == 'French Toast':
subnode = node.find('price')
print "Subnode text: ", subnode.text
subnode.text = '$6.50'
print "Modified subnode text: ", subnode.text
In [33]:
#Add a new subelement to the root
new_name = 'Three-Egg Omlette'
new_price = '$7.95'
new_description = 'three-egg omlette with your choice of meat, cheese and vegetables'
new_calories = '900'
food_node = ET.SubElement(root, 'food', {'name':new_name})
price_subnode = ET.SubElement(food_node, 'price')
price_subnode.text = new_price
description_subnode = ET.SubElement(food_node, 'description')
description_subnode.text = new_description
calories_subnode = ET.SubElement(food_node, 'calories')
calories_subnode.text = new_calories
In [34]:
#Write out the modified xml
tree.write('outputMenu.xml')
Python searches for packages in the following order
This is the local directory and is searched by default
This is a system variable that has the location of modules on your system. Python uses the information stored in that variable to search for modules if they cannot be found in the home directory.
This is searched automatically.
A file added to the top level Python directory that includes a path per line for the location of the packages.
Python adds this automatically when you install 3rd party packages.
In [1]:
# let's look at the system path
import sys
sys.path
Out[1]:
In [2]:
import os
'PYTHONPATH' in os.environ
Out[2]:
In [ ]: