Last active
August 29, 2015 14:08
-
-
Save hyunjun/c929c5655c97711ae62a to your computer and use it in GitHub Desktop.
python lxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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