I have a requirement to replace an expression string for eg: ‘n/7-7’ with ‘div(n,7)-7’ in Python3.9. It could get complicated as well to contain multiple ‘/’ for eg: ‘(n/7-7) +(n/3+3)’ which would become ‘(div(n,7)-7 + ( div(n,3)+3)’. I thought of approaching this problem using tokenize in Python. For eg:
from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
from io import BytesIO
result = []
g = tokenize(BytesIO(expression.encode('utf-8')).readline) # tokenize the string
for toknum, tokval, _, _, _ in g:
result.append((toknum, tokval))
The above result would give some like:
[(62,'utf-8'),(1,'n'), (54,'/'),(2,'7'),(54,'-'),(2,'1'),(4,''),(0,'')]
Idea would be to replace index 1,2,3 in the above list with the below 6 elements:
[(3, 'div'), (54, '('), (1, 'n'), (3, ','), (2, '7'), (54, ')')]
Then I could get the new expression using:
expNew = untokenize(result).decode('utf-8')
Can someone please help how to achieve this using the above method or perhaps a better method?