Python Forum
Escape Single quotation between each content tag
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape Single quotation between each content tag
#4
(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')]]
Reply


Messages In This Thread
RE: Escape Single quotation between each content tag - by snippsat - May-02-2021, 03:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Escape indentation Frankduc 11 2,978 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,140 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  DIY Escape Room for fun StannemanPython 1 2,268 Feb-17-2021, 10:53 PM
Last Post: maurom82
  Something wrong with the quotation mark in dictionary definition Mark17 1 1,958 Jan-29-2021, 03:34 PM
Last Post: buran
  How to escape OrderedDict as an argument? Mark17 2 1,990 Dec-23-2020, 06:47 PM
Last Post: Mark17
  How to instantly add quotation marks and comma for parameters? cheers100 4 7,915 Oct-22-2020, 12:51 PM
Last Post: cheers100
  help for escape sequences NewPi 1 1,997 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  What is the correct syntax for list items that need to contain a quotation mark, etc? KaisoArt 7 3,429 Sep-14-2019, 05:26 AM
Last Post: buran
  escape single quote deep_logic 1 1,766 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020