修复类似JSON的文件中未加引号的键,以便它使用正确的JSON语法 [英] Fix unquoted keys in JSON-like file so that it uses correct JSON syntax

查看:281
本文介绍了修复类似JSON的文件中未加引号的键,以便它使用正确的JSON语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的类似JSON的文件,但是它没有使用正确的JSON语法:未引用对象键.我想编写一个脚本来修复文件,以便可以用json.loads加载它.

I have a very large JSON-like file, but it is not using proper JSON syntax: the object keys are not quoted. I'd like to write a script to fix the file, so that I can load it with json.loads.

我需要匹配所有单词,后跟一个冒号,并用引号引起来的单词替换它们.我认为正则表达式是\w+\s*:,我应该使用re.sub,但是我不确定如何做到这一点.

I need to match all words followed by a colon and replace them with the quoted word. I think the regex is \w+\s*: and that I should use re.sub, but I'm not exactly sure how to do it.

如何获取以下输入并获得给定的输出?

How can I take the following input and get the given output?

# In
{abc : "xyz", cde : {}, fgh : ["hfz"]}
# Out
{"abc" : "xyz", "cde" : {}, "fgh" : ["hfz"]}

# In
{
    a: "b",
    b: {
        c: "d",
        d: []
    },
    e: "f"
}
# Out
{
    "a": "b",
    "b": {
        "c": "d",
        "d": []
    },
    "e": "f"
}

推荐答案

您可以利用以下事实,即日志文件不是有效的JSON,而,而不是潜在的脆弱的正则表达式解决方案. em>有效的 YAML .使用 PyYAML 库,您可以将其加载到Python数据结构中,然后将其写回为有效JSON:

Rather than a potentially fragile regex solution, you can take advantage of the fact that while your log file isn't valid JSON, it is valid YAML. Using the PyYAML library, you can load it into a Python data structure and then write it back out as valid JSON:

import json
import yaml

with open("original.log") as f:
    data = yaml.load(f)

with open("jsonified.log", "w") as f:
    json.dump(data, f)

这篇关于修复类似JSON的文件中未加引号的键,以便它使用正确的JSON语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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