转义字符串中的引号
我有一本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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在替换中转义反斜杠才能将其打印出来。尝试
You need to escape your backslash in the replace in order to get it printed. Try
使用
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
没有必要采取艰难的方式。让 Django 为您序列化查询集。
There's no need to do it the hard way. Let Django serialize the query set for you.
你说:
所以我假设您正在尝试将字符串形式的 Python 对象输出到模板文件中,并进行转换,以便输出字符串是有效的 Javascript 代码。
这相当于将它们序列化为 JSON。
这是一个已解决的问题,您不必用双引号等替换单引号,只需这样做:
这将为您处理所有转义并确保结果是有效的 Javascript 对象。
You say:
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:
This will handle all the escaping for you and ensure that the result is a valid Javascript object.
我有同样的问题,我使用了 python 内置的转义方法。
像这样的事情帮助了我
参考:-
I had same problem, I used a python inbuilt escaping method.
something like this helped me
Ref:-