simple chemistry in python

Brian Blais bblais at bryant.edu
Wed Apr 30 17:35:23 CEST 2008


>
> baoilleach wrote:
>>
>> If you are familiar with parsing XML, much of the data you need is
>> stored in the following file:
>> http://bodr.svn.sourceforge.net/viewvc/*checkout*/bodr/trunk/bodr/ 
>> elements/elements.xml?revision=34&content-type=text%2Fplain


Here's a quick BeautifulSoup script to read it into a python dict.   
It misses anything not a scalar, but you can easily modify it to  
include arrays, etc... in the xml.


hope it's useful.  certainly a neat site!

			bb

-- 
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais


from __future__ import with_statement
from BeautifulSoup import BeautifulSoup

with open('elements.xml') as fid:
     soup=BeautifulSoup(fid)


all_atoms=soup('atom')

element=soup('atom',{'id':'H'})[0]

elements={}
for atom in all_atoms:

     info={}
     id=atom['id']

     scalars=atom('scalar')

     for s in scalars:
         dictref=s['dictref']  # seems to have a bo at the beginning
         datatype=s['datatype'] # seems to have a xsd: at the beginning

         contents=s.contents[0]

         if datatype=='xsd:Integer':
             value=int(contents)
         elif datatype=='xsd:int':
             value=int(contents)
         elif datatype=='xsd:float':
             value=float(contents)
         else:
             value=contents


         key=dictref[3:]

         info[key]=value



     elements[id]=info


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080430/d39a16ba/attachment.html>


More information about the Python-list mailing list