I am parsing a spice file with python which contains string math expressions and I am converting the text file to a python script.
An example of a math expressions in the file is:
expr = 'k1 + 4.35k + 3.69meg*(pow(2.4u, 2*km2))'
I would like to find all the scaling factors in the expression and convert them to their exponent values:
scaling_factors = {
'g' : 'e9',
'meg': 'e6',
'k' : 'e3',
'm' : 'e-3',
'u' : 'e-6',
'n' : 'e-9',
}
my wanted output would be like:
converted_expr = 'k1 + 4.35e3 + 3.69e6*(pow(2.4e-6, 2*km2))'
I tried this:
digits_re = r"([0-9]*\.[0-9]+|[0-9]+)k"
sample = 'k1 + 4.3k'
print(re.sub(digits_re, digits_re.replace('k', 'e3'), sample))
output:
k1 + \\de3
but it doesn't work