May-02-2021, 03:32 PM
(May-02-2021, 08:28 AM)usman Wrote: Is there anyway to only escape only what inside ('content', 'Philp's') content tag. There could multiple content tags in my string.Can use a regex,so this Lookahead and Lookbehind
'
to see that's there a word character on each end.import re import json s = "[[('name', 'productname_0'), ('type', 'html'),('content', 'O'Cornor')],[('name', 'productname_1'), ('type', 'html'), ('content', 'Philp's')]]" result = re.sub(r"(?<=\w)\'(?=\w)", r"\\'", s) print(result) d = json.dumps(result) print(json.loads(d))
Output:[[('name', 'productname_0'), ('type', 'html'),('content', 'O\'Cornor')],[('name', 'productname_1'), ('type', 'html'), ('content', 'Philp\'s')]]
[[('name', 'productname_0'), ('type', 'html'),('content', 'O\'Cornor')],[('name', 'productname_1'), ('type', 'html'), ('content', 'Philp\'s')]]