Skip to content Skip to sidebar Skip to footer

How To Escape Single Quotes In Python On A Server To Be Used In Javascript On A Client

Consider: >>> sample = 'hello'world' >>> print sample hello'world >>> print sample.replace(''','\'') hello'world In my web application I need to store m

Solution 1:

As a general solution for passing data from Python to Javascript, consider serializing it with the json library (part of the standard library in Python 2.6+).

>>>sample = "hello'world">>>import json>>>print json.dumps(sample)
"hello\'world"

Solution 2:

Use:

sample.replace("'", r"\'")

or

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

Post a Comment for "How To Escape Single Quotes In Python On A Server To Be Used In Javascript On A Client"