Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping quotes in string

I have a python dictionary e.g.:

[{"pk":"1","name":"John","size":"1/4" "},{},{},etc]

That size is 1/4 inch,how would I "escape" that quote? So it still would display it as 1/4",

Its a list of things, so I cant just manually code it like 1/4\", I tried replace('"','\"')

EDIT: The orginal list is a textfield in my Django models:

[{'pk': '91', 'size': '', 'name': 'Thread Flat For BF', 'quantity': '2'}, {'pk': '90', 'size': '', 'name': 'Blade Holders Straight ', 'quantity': '26'},{'size':'3"','name':'2m 1/4" Round bar', 'quantity':'43'},{'size':'5','name':'2m 1/8" Round bar', 'quantity':'4'}]

Next step I have to prepare the list for jQuery, so I replace like this so its in the correct syntax for json. mat_list = manufactured_part.material_list.replace("'",'"')

Then I have this list:

[{"pk": "91", "size": "", "name": "Thread Flat For BF", "quantity": "2"}, {"pk": "90", "size": "", "name": "Blade Holders Straight ", "quantity": "26"},{"size':"3"","name':"2m 1/4" Round bar", "quantity":"43"},{"size":"5","name":"2m 1/8" Round bar", "quantity":"4"}]

So now the list is sent to the template and I loop through it with jquery, but the list is broken because of the " in the strings.

SO...I need to escape those " for the list to work, otherwise it has an obvious syntax error.

Hope this makes sense now.

Thanks

like image 586
Harry Avatar asked Jun 08 '11 07:06

Harry


People also ask

How do you escape quotation marks 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 ( ' ).

What is escaping a string?

What is "Escaping strings"? Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string, you typically surround it in either double quotes or single quotes: "Hello, World."

How do you escape a quote from a string in Java?

The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include: Carriage return and newline: "\r" and "\n" Backslash: "\\"


3 Answers

You need to escape your backslash in the replace in order to get it printed. Try

replace('"','\\"')
like image 116
stema Avatar answered Oct 04 '22 17:10

stema


Using

shlex.quote("string")

or

pipes.quote("string")

Depending on the python version worked for me.

You can check here more details

https://github.com/python/cpython/blob/master/Lib/shlex.py#L281

like image 41
Fabián Bertetto Avatar answered Oct 04 '22 15:10

Fabián Bertetto


There's no need to do it the hard way. Let Django serialize the query set for you.

like image 31
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 15:10

Ignacio Vazquez-Abrams