[Tutor] how to extract text by specifying an element using ElementTree

Kent Johnson kent37 at tds.net
Tue Dec 20 18:58:40 CET 2005

ps python wrote:
> Dear Drs. Johnson and Yoo , 
>  for the last 1 week I have been working on parsing
> the elements from a bunch of XML files following your
> suggestions. 
> from elementtree.ElementTree import ElementTree
>>>>mydata = ElementTree(file='00004.xml')
>>>>for process in
> mydata.findall('//biological_process'):
> 	print process.text

Looking at the data, neither <biological_process> nor <functions> elements directly
contain text, they have children that contain text. Try
   print process.get('title').text
to print the title.

>>>>for proc in mydata.findall('functions'):
> 	print proc

I think you want findall('//functions') to find <functions> at any depth in the tree.

If this doesn't work please show the results you get and tell us what you expect.


More information about the Tutor mailing list