[Solved] Replacing part of string python [closed]


For the simplest case, you might solve this with a regular expression:

>>> import re
>>> re.sub(r"('?null'?)", "'Null'", "{'abcd': null}")
"{'abcd': 'Null'}"
>>> re.sub(r"('?null'?)", "'Null'", "{'abcd': 'null'}")
"{'abcd': 'Null'}"
>>> 

but from the look of you example and the comment (which should really be part of your question) mentionning you might have a lot more in the string, I suspect you are dealing with json here. If that’s the case, the proper solution (the only robust solution actually) is to deserialize your string to Python (using json.loads(...)), update the resulting Python object (usually a dict, but might be a list of dicts or might contains lists containing dicts etc), then serialize it back with json.dumps().

2

solved Replacing part of string python [closed]