BorDeh added a comment.

Here is a small script to get molecular mass from chemical formula. It used the python package periodic to get the element masses (pip install periodic to get it)

import re
from periodic import element

def calcmolmass(chemformula):
    subparts = re.findall('[A-Z][^A-Z]*', chemformula)
    firstdigits = [re.search("\d", sp) for sp in subparts]
    firstdigits = [fd.start() if fd is not None else None for fd in firstdigits]
    nums = [float(sp[fd:]) if fd is not None else 1  for sp, fd in zip(subparts, firstdigits)]
    els  = [sp[:fd]        if fd is not None else sp for sp, fd in zip(subparts, firstdigits)]
    mass = sum(element(el).mass*num for el,num in zip(els,nums))
    return mass

print(calcmolmass('C3H6O'))

TASK DETAIL
https://phabricator.wikimedia.org/T208036

EMAIL PREFERENCES
https://phabricator.wikimedia.org/settings/panel/emailpreferences/

To: BorDeh
Cc: BorDeh, Vemonet, Freddytuxworth, Husky, Laffano, S9a8m, A_ka_es, Teffubud, Dinadineke, Dja, Elvalente, Nandana, tabish.shaikh91, Lahi, Gq86, GoranSMilovanovic, Soteriaspace, Jayprakash12345, JakeTheDeveloper, QZanden, merbst, LawExplorer, DDJJ, Harmonia_Amanda, Spinster, Jane023, Wikidata-bugs, aude, TheDJ, Mbch331, valhallasw
_______________________________________________
Wikidata-bugs mailing list
Wikidata-bugs@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikidata-bugs

Reply via email to