Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Last active August 29, 2015 14:08
Show Gist options
  • Save hyunjun/c929c5655c97711ae62a to your computer and use it in GitHub Desktop.
Save hyunjun/c929c5655c97711ae62a to your computer and use it in GitHub Desktop.
python lxml
# -*- coding: utf8 -*-
# http://lxml.de/tutorial.html
from lxml import etree
with open('test.xml', 'r') as f:
'''doc = etree.parse(f)
for elem in doc.iter('*'):
print elem.tag, elem.text.encode('utf8')
for k, v in elem.items():
print ('\t%s\t%s' % (k, v)).encode('utf8')'''
lines = ''.join([l for l in f.readlines()])
doc = etree.XML(lines)
price = doc.find('.//price')
print price.tag, price.text
min = doc.find('.//price/min')
print min.tag, min.text
max = doc.find('.//price/max')
print max.tag, max.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment