我有一个用于json.dump()的非法字符串,我需要对其进行编辑。我需要将数字替换为从0:
到"0:"
的字符串,以便进行扩展。与1
和后面的其他数字相同。
此字符串不时更改,数字也可以更改
{"aclList": {0: {"sequence": {1: {"counterData": …etc etc
必须成为:
{"aclList": {"0": {"sequence": {"1": {"counterData": …etc etc
我认为我需要找到\d:
并用他自己加上qoutes ""
答案 0 :(得分:1)
这应该可以解决您99%的问题:
(?<={)(\d+)(?=:)
https://regex101.com/r/96ReYn/1/(有关代码,请参见https://regex101.com/r/96ReYn/1/codegen?language=python)
(?<={)
-确保上一个字符为大括号(\d+)
-抓住所有数字(?=:)
-确保下一个字符是冒号它将失败的1%用于诸如"I'm going to mess {3: up your day"
之类的数据,因此希望它在您的“ JSON”中不存在。
答案 1 :(得分:-1)
是的,您可以这样做:
s = '{"aclList": {0: {"sequence": {1: {"counterData": …etc etc'
for x in re.findall('\d', s):
s = s.replace(x, f'"{x}"')