I have the following string,
test = "if 'col.A' not null and if 'col.B' not 'null'
I want to add str(row[...] for the tokens which are encapsulated within '' and contains a . in the token. How can I achieve this in python, I'm not a regex pro. Prior to this I was manually replacing, but since the number elements within '' are to increase, I want to automate it. I want the end result to be like,
test = "if str(row['col.A']) not null and if str(row['col.B']) not 'null'
Thanks!
CodePudding user response:
Try:
import re
test = "if 'col.A' not null and if 'col_B' not null"
out = re.sub(r"'([^']*\.[^']*)'", r"str(row['\1'])", test)
print(out)
# Output:
if str(row['col.A']) not null and if 'col_B' not null
