So I'm kind of new to Python. Right now I'm making a chemical equation balancer and I've got stuck because what I want to do right now is that if you receive a compound in parenthesis, with a subindex outside (like this: (NaCl)2), I want to expand it to this form: Na2Cl2 (and also get rid of the parenthesis). Right now I've managed just to get rid of the parenthesis with this code:
import string
import re
linealEquation = ""
def linealEq(equation):
#missing code
allow = string.letters + string.digits + '+' + '-' + '>'
linealEquation = re.sub('[^%s]' % allow, '', equation)
print linealEquation
linealEq("(CrNa)2 -> Cr+Na")
But how can I trace the string and multiply the indexes out of the parenthesis?
I know how to iterate over a string, but I cannot think of how to specifically multiply the sub index.
Thanks for the help.