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
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
  DIY Escape Room for fun StannemanPython 1 496 Feb-17-2021, 10:53 PM
Last Post: maurom82
  Something wrong with the quotation mark in dictionary definition Mark17 1 488 Jan-29-2021, 03:34 PM
Last Post: buran
  How to escape OrderedDict as an argument? Mark17 2 477 Dec-23-2020, 06:47 PM
Last Post: Mark17
  How to instantly add quotation marks and comma for parameters? cheers100 4 682 Oct-22-2020, 12:51 PM
Last Post: cheers100
  help for escape sequences NewPi 1 755 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 1,417 Sep-14-2019, 05:26 AM
Last Post: buran
  escape single quote deep_logic 1 723 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  The use of escape char \ hishamzero1 2 980 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  Escape sequences display in python Uchikago 1 877 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Python 3 escape codes oldDog 1 1,244 Sep-15-2018, 10:12 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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