Created
January 21, 2020 09:29
-
-
Save Pranjal-R-Agrawal/812e429c79e8aa67fa6c55b2d1dc6f8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_compound(formula): | |
f_formula = "" | |
for part in formula.split("."): | |
for i in range(0, len(part)): | |
if i + 1 < len(part): | |
if part[i].isalpha() and (part[i + 1].isupper() or part[i + 1] in ["(", "[", ")", "]"]): | |
f_formula += part[i] + "1" | |
elif part[i] in [")", "]"] and part[i + 1].isalpha(): | |
f_formula += part[i] + "1" | |
else: | |
f_formula += part[i] | |
else: | |
if part[i].isalpha() or part[i] in [")", "]"]: | |
f_formula += part[i] + "1" | |
else: | |
f_formula += part[i] | |
f_formula += ("." if len(formula.split(".")) > 1 else "") | |
if f_formula.find(".") > -1: | |
part = f_formula.split(".") | |
val = str(re.findall('[0-9][0-9]*', part[1])[0]) | |
val = ("1" if val == "" else val) | |
f_formula = part[0] + \ | |
"(" + str(re.findall('[A-Za-z]\S*', part[1])[0]) + ")" + val | |
return f_formula |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment