Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all quotes in a string with escaped quotes?

Given a string in python, such as:

s = 'This sentence has some "quotes" in it\n'

I want to create a new copy of that string with any quotes escaped (for further use in Javascript). So, for example, what I want is to produce this:

'This sentence has some \"quotes\" in it\n'

I tried using replace(), such as:

s.replace('"', '\"')

but that returns the same string. So then I tried this:

s.replace('"', '\\"')

but that returns double-escaped quotes, such as:

'This sentence has some \\"quotes\\" in it.\n'

How to replace " with \"?

UPDATE:

I need as output from this copyable text that shows both the quotes and the newlines as escaped. In other words, I want to be able to copy:

'This sentence has some \"quotes\" in it.\n'

If I use the raw string and print the result I get the correctly escaped quote, but the escaped newline doesn't print. If I don't use print then I get my newlines but double-escaped quotes. How can I create a string I can copy that shows both newline and quote escaped?

like image 379
mix Avatar asked Sep 19 '13 05:09

mix


People also ask

How do you escape quotes in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do you escape quotes within a quote?

It's done by finishing an already-opened one ( ' ), placing the escaped one ( \' ), and then opening another one ( ' ). It's done by finishing already opened one ( ' ), placing a quote in another quote ( "'" ), and then opening another one ( ' ).

How do you escape characters with double quotes?

To cause the C compiler to interpret the double quotation mark as a character, precede the double quotation mark with the C escape character, the backslash (\).

How do you escape quotes in HTML?

JavaScript Strings Escaping quotes Quotes in HTML strings can also be represented using ' (or ' ) as a single quote and " ( or " ) as double quotes. Note: The use of ' and " will not overwrite double quotes that browsers can automatically place on attribute quotes.


Video Answer


3 Answers

Hi usually when working with Javascript I use the json module provided by Python. It will escape the string as well as a bunch of other things as user2357112 has pointed out.

import json
string = 'This sentence has some "quotes" in it\n'
json.dumps(string) #gives you '"This sentence has some \\"quotes\\" in it\\n"'
like image 147
Yussuf S Avatar answered Oct 16 '22 11:10

Yussuf S


Your second attempt is correct, but you're getting confused by the difference between the repr and the str of a string. A more idiomatic way of doing your second way is to use "raw strings":

>>> s = 'This sentence has some "quotes" in it\n'
>>> print s
This sentence has some "quotes" in it

>>> print s.replace('"', r'\"')  # raw string used here
This sentence has some \"quotes\" in it

>>> s.replace('"', r'\"')
'This sentence has some \\"quotes\\" in it\n'

Raw strings are WYSIWYG: backslashes in a raw string are just another character. It is - as you've discovered - easy to get confused otherwise ;-)

Printing the string (the 2nd-last output above) shows that it contains the characters you want now.

Without print (the last output above), Python implicitly applies repr() to the value before displaying it. The result is a string that would produce the original if Python were to evaluate it. That's why the backlashes are doubled in the last line. They're not in the string, but are needed so that if Python were to evaluate it each \\ would become one \ in the result.

like image 27
Tim Peters Avatar answered Oct 16 '22 11:10

Tim Peters


Your last attempt was working as you expected it to. The double backslashes you see are simply a way of displaying the single backslashes that are actually in the string. You can verify this by checking the length of the result with len().

For details on the double backslash thing, see: __repr__()


UPDATE:

In response to your edited question, how about one of these?

print repr(s).replace('"', '\\"')
print s.encode('string-escape').replace('"', '\\"')

Or for python 3:

print(s.encode('unicode-escape').replace(b'"', b'\\"'))
like image 7
ʇsәɹoɈ Avatar answered Oct 16 '22 12:10

ʇsәɹoɈ