1

Hi I am trying to escape a single quotation between specific text.

My String

a = "{'content': 'Roberto Delacruz'}"

If I use this command

a=a.replace(r"'", r"\'")

It gives me following output

"{\\'content\\': \\'Roberto Delacruz\\'}"

How to I escape a single quote between this string

content':''

Any single quote between above specified text should be escaped.

Example: content':'O'Cornor' (This name has single quote).

Some Additional Explanation My Actual String looks like this "[('name', 'product'), ('type', 'html'),('content', 'O'Cornor')]" O'Cornor is a name of person which will be inside the single quotation. Also this name has single Quote also. I need to escape this Single Quote only. This string will be utilized in Command line

python -m myscript.py "[('name', 'product'), ('type', 'html'),('content', 'O'Cornor')]"

adnan
  • 504
  • 1
  • 4
  • 21
  • Can you provide an explanation of how you want to utilize the result of escaping a single quote. Is it so that the result can be printed, stored in a database, stored in a DataFrame or something else? – itprorh66 May 01 '21 at 17:39
  • I added some explanation for the string. – adnan May 01 '21 at 18:36
  • Who/what is generating the string? How are you processing it in `myscript.py`? This sounds like "command injection on purpose" – why are the values in the string not properly escaped for their intended context? – knittl May 07 '21 at 17:37

3 Answers3

0

Using the command

a=a.replace(r"'", r"\'")

will give you just what you want. But you have to use the following:

print(a)

to see the actual result. just entering a on the interpreter will show you the repr version of the string. The below image will make it clear.

enter image description here

For more information see here.

avasuilia
  • 136
  • 4
0

For the second part of your question, you can use regex:

import re
a="[('name', 'product'), ('type', 'html'),('content', 'O'Cornor')]"
a=re.sub(r", '([^\(\),]*)'([^\(\),]*)'\)", r", '\1\\'\2'", a)
print(a) #[('name', 'product'), ('type', 'html'),('content', 'O\'Cornor']

For more information about replacing substrings of a string using regex see here.

avasuilia
  • 136
  • 4
0

This is not something you can do after the fact …

python -m myscript.py "[('name', 'product'), ('type', 'html'),('content', 'O'Cornor')]"

Here, the String is not 'O'Cornor', but instead it is 'O' followed by invalid syntax. The input cannot be parsed, your program will not compile. It will never run, therefore your regex would never be executed. You need to provide properly terminated strings in your source. Multiple options:

python -m myscript.py "[('name', 'product'), ('type', 'html'),('content', 'O\\'Cornor')]" # double backslash required for shell: \\' in the shell string literal becomes \' in the python string literal, becomes ' in the final python string value
python -m myscript.py "[('name', 'product'), ('type', 'html'),('content', \"O'Cornor\")]"
python -m myscript.py '[("name", "product"), ("type", "html"),("content", "O'"'"'Cornor")]'

Try it out:

$ python -c "print('O'Cornor')"
File "<string>", line 1
    print('O'Cornor')
             ^
SyntaxError: invalid syntax

$ python -c "print(\"O'Cornor\")"
O'Cornor

$ python -c "print('O\\'Cornor')"
O'Cornor
knittl
  • 246,190
  • 53
  • 318
  • 364