Reputation: 1877
I have a string, like this:
{"content":(uint64)123456, "id":(uint32)0}
Note:
this example string is simple, the real string is JSON except (uint32)0
is not standard.
Now I need to transform it like this:
{"content":"(uint64)123456", "id":"(uint32)0"}
so, I write transform code with python re:
def format():
pattern = re.compile(r'(\(uint32\)|\(int32\)|\(uint64\)|\(int64\))(\d)+')
print pattern.sub('\"test\"', '{"content":(uint64)123456, "id":(uint32)0}')
How I should write code in sub
function in order to tansform it?
Upvotes: 0
Views: 44
Reputation: 19352
Instead of a string, sub
function can accept also a callable as replacement.
So, instead of:
pattern.sub('\"test\"', s)
Make a function:
def add_quotes(match):
return '"%s"' % match.group(0)
pattern.sub(add_quotes, s)
Upvotes: 1