如何编辑非标准JSON文件或正确访问其值

时间:2018-06-21 03:10:36

标签: python json parsing

我有一个非标准格式的JSON文件,如下所示:

{
    "color": "black",
    "category": "hue",
    "type": "primary"
}
{
    "color": "white",
    "category": "value",
    "type": "idk"
}
{
    "color": "red",
    "category": "hue",
    "type": "primary"
}

除了它有超过10,000个以这种方式格式化的条目外。我必须访问每个单独部分的颜色和类型,并为每个显示“类型颜色”(例如“原色黑色”)的字符串创建一个字符串。

我想使用Python编辑文件,如下所示:

[
    {
        "color": "black",
        "category": "hue",
        "type": "primary"
    },
    {
        "color": "white",
        "category": "value",
        "type": "idk"
    },
    {
        "color": "red",
        "category": "hue",
        "type": "primary"
    },
]

因此,我可以使用[]访问值,并对所有值使用for循环。我该怎么做?有比编辑json文件更好的方法吗?

2 个答案:

答案 0 :(得分:0)

文件是JSON还是不是,而您提供的示例文件不是。将其称为“非标准JSON”是不准确和令人困惑的。

也就是说,这是我将如何使用Python将文件转换为真正的JSON的概述:

Open a new file for writing.
Write "[" to the output file.
Open your existing file for reading.
For each line in the file:
    Write that line to the output file.
    If that line is "}", but it is not the last line, also write a comma.
Write ] to the output file.

答案 1 :(得分:0)

您可以使用str.format

In [341]: with open('f.json', 'r') as f:
     ...:     string = f.read()
     ...:

In [342]: string
Out[342]: '{\n    "color": "black",\n    "category": "hue",\n    "type": "primary"\n}\n{\n    "color": "white",\n    "category": "value",\n    "type": "idk"\n}\n{\n    "color": "red",\n    "category": "hue",\n    "type": "primary"\n}\n'

In [343]: string = string.replace('}','},') # Must split each '{}' with a comma

In [344]: string
Out[344]: '{\n    "color": "black",\n    "category": "hue",\n    "type": "primary"\n},\n{\n    "color": "white",\n    "category": "value",\n    "type": "idk"\n},\n{\n    "color": "red",\n    "category": "hue",\n    "type": "primary"\n},\n'

In [345]: string = string[:-2] # Get rid of the trailing comma

In [346]: string
Out[346]: '{\n    "color": "black",\n    "category": "hue",\n    "type": "primary"\n},\n{\n    "color": "white",\n    "category": "value",\n    "type": "idk"\n},\n{\n    "color": "red",\n    "category": "hue",\n    "type": "primary"\n}'

In [347]: json.loads('[{0}]'.format(string))
Out[347]:
[{'color': 'black', 'category': 'hue', 'type': 'primary'},
 {'color': 'white', 'category': 'value', 'type': 'idk'},
 {'color': 'red', 'category': 'hue', 'type': 'primary'}]