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
#1
Hello All,

I am trying to escape a Quotation between content tag of this string

"[[('name', 'productname_0'), ('type', 'html'),('content', 'O'Cornor')],[('name', 'productname_1'), ('type', 'html'), ('content', 'Philp's')]]"


Previously I had this string
"[[('name', 'productname_0'), ('type', 'html'),('content', 'A Cool Shirt')],[('name', 'productname_1'), ('type', 'html'), ('content', 'A Cool Shirt')]]" 
Its working fine. But When I added Single Quotation inside quotation 'O'Cornor'
Example : ('content', 'O'Cornor')

Its giving me syntax error issue. How Can I escape Single Quotation between content value only

*content is Key
* O'Cornor is value between single quotation
Reply
#2
escape with \ or use double quotes.
print('This is Tom\'s car')
#or
print("This is Tom's car")
Output:
This is Tom's car This is Tom's car
snippsat likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(May-01-2021, 09:35 PM)menator01 Wrote: escape with \ or use double quotes.
print('This is Tom\'s car')
#or
print("This is Tom's car")
Output:
This is Tom's car This is Tom's car

Thank you. Is there anyway to only escape only what inside ('content', 'Philp's') content tag. There could multiple content tags in my string.
Reply
#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


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,976 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,912 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,425 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