通过 python for 循环获取无效的 json 文件

Getting invalid json file by python for loop

你好,首先我想做一个网络抓取机器人,然后将所有信息保存在一个 json 文件中 但是循环时我的 json 无效

这是我的代码的一部分,也是我生成的地方 json

for tag in tags:
             myarr=tag.getText(strip=True)
             words=myarr.split()
             titles = []
             titles.append(words)
             data = [{"data": w} for w in zip(titles)]



             with open('data.json', 'a+',encoding='utf-8') as f:  

              json.dump(data, f,indent=2, ensure_ascii=False)

这是由 python

生成的无效 json 文件的一部分
[
  {
    "data": [
      [
        "Acuña",
        "Game",
        "GermánEspecialidad:Tratamiento",
        "del",
        "DolorLugar",
        "de",
        "Atención:Centro",
        "de",
        "Diagnóstico",
        "1"
      ]
    ]
  }
][
  {
    "data": [
      [
        "Aguayo",
        "Baeza",
        "EdgardoEspecialidad:Reumatología",
        "AdultosLugar",
        "de",
        "Atención:Centro",
        "de",
        "Diagnóstico",
        "1",
        "Piso",
        "7"
      ]
    ]
  }
]

尝试在在线 json 解析器上解析此 json 时,它显示 SyntaxError: Unexpected token [ in JSON at position 318

是否可以生成有效的 json? 如果是,有人可以帮助我吗?

Python 的 json 模块不直接支持增量构建 json 文件*。有效的 json 可以通过将每个字典附加到一个列表,然后将列表转储到输出文件来生成,如下所示:

data = []
for tag in tags:
     myarr=tag.getText(strip=True)
     words=myarr.split()
     titles = []
     titles.append(words)
     data.extend({"data": w} for w in zip(titles))

# Once all the data has been processed, write to file.  
with open('data.json', 'w',encoding='utf-8') as f:  
    json.dump(data, f,indent=2, ensure_ascii=False)

* 来自 json.dump 的文档:

...trying to serialize multiple objects with repeated calls to dump() using the same fp will result in an invalid JSON file