XML parsing per record

Willem Ligtenberg WLigtenberg at gmail.com
Fri Apr 22 13:25:08 EDT 2005


Is there an easy way, to couple data together. Because I have discoverd an
irritating feature in the xml file.
Sometimes this is a database reference:
<Dbtag>
	<Dbtag_db>UCSC</Dbtag_db>
	<Dbtag_tag>
		<Object-id>
			<Object-id_str>1234</Object-id_str>
		</Object-id>
	</Dbtag_tag>
</Dbtag>

And sometimes:

<Dbtag>
	<Dbtag_db>UCSC</Dbtag_db>
	<Dbtag_tag>
		<Object-id>
			<Object-id_id>1234</Object-id_id>
		</Object-id>
	</Dbtag_tag>
</Dbtag>

So I get a list database names and two! lists of ID's
And those two are in no way related. Is there an easy way to create a
dictionary like this DBname --> ID
If not, I still might need to revert to SAX... :(

On Fri, 22 Apr 2005 15:56:29 +0200, Willem Ligtenberg wrote:

> As you can read in the other post of mine, my problem was with the
> iterating through the list. didn't know that you should do. e.text. I did
> only print e, not print e.text
> Did read documentation, but must admit not everything.
> 
> Anyway, thank you very much!
> 
> On Fri, 22 Apr 2005 15:47:08 +0200, Fredrik Lundh wrote:
> 
>> Willem Ligtenberg wrote:
>> 
>>> As I'm trying to write the code using cElementTree.
>>> I stumble across one problem. Sometimes there are multiple values to
>>> retrieve from one record for the same element. Like this:
>>> <Prot-ref_name_E>ATP-binding cassette, subfamily G, member 1</Prot-ref_name_E>
>>> <Prot-ref_name_E>ATP-binding cassette 8</Prot-ref_name_E>
>>> 
>>> How do you get not only the first, but the rest as well, so that I can
>>> store it in a list.
>> 
>> findall returns a list of matching elements.  if "elem" is the paretnt element,
>> this gives you a list of the text inside all Prot-ref_name_E child elements:
>> 
>>     [e.text for e in elem.findall("Prot-ref_name_E")]
>> 
>> (you have read the elementtree documentation, I hope?)
>> 
>> </F>




More information about the Python-list mailing list