简体   繁体   English

如何在python中使用正则表达式re.sub()的组之前插入反斜杠

[英]How to insert backslash before a group with regex re.sub() in python

I have a text string that I want to convert to JSON, like:我有一个要转换为 JSON 的文本字符串,例如:

{text1: text2}

however text2 is filled with illegal characters: "{[]}, so it won't be parsed correctly.但是 text2 填充了非法字符:“{[]},因此无法正确解析。

I would like to escape each illegal character by inserting a backslash before it, but I can't get it to work.我想通过在每个非法字符之前插入一个反斜杠来转义它,但我无法让它工作。

The closest I can get is :我能得到的最接近的是:

In [6]: re.sub('([\[\]\{\},"]{1})', r'\\\1', 'abc[def') 
Out[6]: 'abc\\[def'

But this inserts two backslashes instead of one... I can't get it to insert one.但这插入了两个反斜杠而不是一个......我无法让它插入一个。

On second though, perhaps the problem is with my json.loads()?不过,第二个问题可能出在我的 json.loads() 上? Here's an example:这是一个例子:

In [41]: z
Out[41]: '{"abc": "sdfd\\[sfsdfdf"}'
In [42]: print(z)
Out[42]: {"abc": "sdfd\[sfsdfdf"}

As you can see by the difference between z and print(z), the backslash is properly escaped.从 z 和 print(z) 之间的区别可以看出,反斜杠已正确转义。 But when I execute但是当我执行

json.loads(z)

I still get the Invalid escape error on the backslash.我仍然在反斜杠上收到 Invalid escape 错误。

Any ideas?有任何想法吗?

You don't need to escape brackets for JSON .您不需要为JSON转义括号。 JSON expects a unicode character number or " , \ and widespaces. The problem is rather how Python handles escape sequences in strings. Just feed it as raw string to json.loads() : JSON 需要一个 unicode 字符号或"\和宽空格。问题在于 Python 如何处理字符串中的转义序列。只需将其作为原始字符串提供给json.loads()

import json

json.loads(r'{"abc": "abc[def"}')
json.loads(r'{"abc": "ab\\cd\"e\tf"}')
json.loads('{"abc": "abc'+ re.escape('abc\def') +'def"}')

would print:会打印:

{'abc': 'abc[def'} {'abc': 'abc[def'}
{'abc': 'ab\cd"e\tf'} {'abc': 'ab\cd"e\tf'}
{'abc': 'abcabc\defdef'} {'abc': 'abcabc\defdef'}

So you can keep your code but you need to escape the right characters:所以你可以保留你的代码,但你需要转义正确的字符:

import json
import re

json.loads(r'{"abc": "' + re.sub(r'\\', r'\\\\', 'abc\def') + '"}')

{'abc': 'abc\def'} {'abc': 'abc\def'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM