Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python lxml and stdin

Tags:

python

xml

lxml

I have a xml file, book.xml (http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx)

I would like to cat books.xml and get all book ids and genres for the book id.

Similar to

cat books.xml | python reader.py

Any tips or help would be appreciated. Thanks.

like image 328
Ananymous Avatar asked Sep 17 '10 12:09

Ananymous


1 Answers

To read an XML file from stdin, just use etree.parse. This function accepts a file object, which can be sys.stdin.

import sys
from lxml import etree

tree = etree.parse(sys.stdin)

print ( [(b.get('id'), b.findtext('genre')) for b in tree.iterfind('book')] )
like image 176
kennytm Avatar answered Sep 27 '22 19:09

kennytm