字符串中的转义引号 [英] Escaping quotes in string

查看:108
本文介绍了字符串中的转义引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python字典,例如:

I have a python dictionary e.g.:

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

该尺寸为1/4英寸,我如何转义该引号?因此它仍将其显示为1/4,

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

它是事物列表,所以我不能像 1 / 4\
这样手动编码,我尝试了 replace('','\ ')

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

编辑:
原始列表是我的Django模型中的一个文本字段:

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'}]

下一步,我必须为jQuery准备列表,因此我将其替换为使用json的正确语法。
mat_list = Manufactured_pa​​rt.material_list.replace(','')

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("'",'"')

然后我有此列表:

[{"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"}]

现在,列表已发送到模板,并使用jquery遍历它,但是列表由于字符串中的而损坏。

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 ...我需要对那些字符串进行转义,以使列表起作用,否则它将出现明显的语法错误。

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

希望现在这很有意义。

谢谢

推荐答案

您需要在替换中转义反斜杠才能打印出来。尝试

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

replace('"','\\"')

这篇关于字符串中的转义引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆