3

I have the following string

test = "if row['adm.w'] is 'Bad' and row['rem'] not empty"

I want to add str(...) for those tokens which are row[...], how can I do this in regex? I came up with this and it's not working as intended:

re.sub(r"'([^row[']*)'", r"str(['\1'])", test)

I want the end result to be

test = "if str(row['adm.w']) is 'Bad' and str(row['rem']) not empty"

1 Answer 1

3

This should work:

>>> re.sub(r"(row\[.*?\])", r"str(\1)", test)
"if str(row['adm.w']) is 'Bad' and str(row['rem']) not empty"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.