转义字符串中的引号

发布于 2024-11-14 16:41:44 字数 1125 浏览 3 评论 0原文

我有一本Python字典,例如:

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

尺寸是1/4英寸,我如何“转义”这句话?所以它仍然会将其显示为 1/4",

它是一个列表,所以我不能像 1/4\" 那样手动编码它, 我尝试 replace('"','\"')

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

[{'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 = Manufacturing_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 循环它,但列表由于 " 中的 " 被破坏了字符串。

所以......我需要转义这些“才能使列表正常工作,否则它有一个明显的语法错误。

希望现在这有意义。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈年往事 2024-11-21 16:41:44

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

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

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

replace('"','\\"')
终陌 2024-11-21 16:41:44

使用

shlex.quote("string")

pipes.quote("string")

取决于对我有用的 python 版本。

您可以在此处查看更多详细信息

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

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

倾城月光淡如水﹏ 2024-11-21 16:41:44

没有必要采取艰难的方式。让 Django 为您序列化查询集。

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

错爱 2024-11-21 16:41:44

你说:

我必须为 jQuery 准备列表

所以我假设您正在尝试将字符串形式的 Python 对象输出到模板文件中,并进行转换,以便输出字符串是有效的 Javascript 代码。

这相当于将它们序列化为 JSON。

这是一个已解决的问题,您不必用双引号等替换单引号,只需这样做:

import json

my_python_data = [{'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'}]

str_to_output_in_js_template = json.dumps(my_python_data)

这将为您处理所有转义并确保结果是有效的 Javascript 对象。

You say:

I have to prepare the list for jQuery

So I assume you are trying to output Python objects in string form into a template file, transformed so that the output string is valid Javascript code.

This is equivalent to serializing them as JSON.

This is a solved problem, instead of replacing single quotes with double quotes etc yourself just do this:

import json

my_python_data = [{'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'}]

str_to_output_in_js_template = json.dumps(my_python_data)

This will handle all the escaping for you and ensure that the result is a valid Javascript object.

请帮我爱他 2024-11-21 16:41:44

我有同样的问题,我使用了 python 内置的转义方法。
像这样的事情帮助了我

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

参考:-

http://docs.python.org/2/reference/ lexical_analysis.html#string-literals

I had same problem, I used a python inbuilt escaping method.
something like this helped me

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

Ref:-

http://docs.python.org/2/reference/lexical_analysis.html#string-literals

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文