解析堆栈JSON时缺少逗号,并将其保存在单独的fi中

2024-09-30 18:12:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看一份文件(测试.json)包含不以逗号分隔的JSON对象

{
   "ID": "349878",
   "Name": user1
   "object_name": [
        "Vessel",
        "Sherds"]
}
{
   "ID": "349879",
   "Name": user2
}
{
   "ID": "349880",
   "Name": user3
}

我希望将每个对象存储在单独的文件中—其中ID是其文件名。

示例:文件349878.json应包含

^{pr2}$

Tags: 文件对象nameidjsonobject文件名逗号
3条回答

我不知道为什么你的json是无效的,因为你用“NOT”分隔逗号作为要求,我希望这一条能说明你的问题。在

import re

regex = r"\{(.*?)\}"

test_str = ("{\n"
            '"ID": "349878",\n'
            '"Name": user1\n'
            '"object_name": [\n'
            '"Vessel",\n'
            '"Sherds"]\n'
            "}\n"
            "{\n"
            '"ID": "349879",\n'
            '"Name": user2\n'
            "}\n"
            "{\n\n"
            '"ID": "349880",\n'
            '"Name": user3\n'
            "}")

matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)

for matchNum, match in enumerate(matches):
    for groupNum in range(0, len(match.groups())):
        with open("{}.txt".format(match.group(1)[7:17].replace(",", "").strip()), 'w') as fout:
            fout.write(match.group(0))

假设您的JSON数据正确验证如下:

[
    {
       "ID": "349878",
       "Name": "user1",
       "name": [
            "Vessel",
            "Sherds"]
    },
    {
       "ID": "349879",
       "Name": "user2"
    },
    {
       "ID": "349880",
       "Name": "user3"
    }
]

可以用JSON Formatter and Validator验证。在

您可以从^{}提取每个JSON对象,并用^{}将每个对象写入一个单独的文件:

^{pr2}$

将生成以下JSON文件:

349878.json

{
    "ID": "349878",
    "Name": "user1",
    "name": [
        "Vessel",
        "Sherds"
    ]
}

349879.json

{
    "ID": "349879",
    "Name": "user2"
}

349880.json

{
    "ID": "349880",
    "Name": "user3"
}

你可以用结构分裂()并使用切片查找id并创建文件。如果不删除空白,可以使用不同的索引。在

with open('test.json', 'r') as file:
# Get text without whitespace or newlines
text = file.read().replace(' ', '').replace('\n', '')
# Split by '{', discard first entry (will be empty)
objects = text.split('{')[1:]

for object in objects:
    # Add the split delimiter back
    object = '{' + object
    # Get the id relative to the json data
    id = object[ object.find('"ID"') + 6 :
                object.find('"Name"') - 2 ]
    # Add the file extension
    id += '.json'

    # If the file doesnt exist, create it and write the data
    with open(id, 'x') as file:
        file.write(object)

相关问题 更多 >