简体   繁体   English

如果值不存在,则 Python JSON 追加

[英]Python JSON append if value doesn't exist

I've got a json file with 30-ish, blocks of "dicts" where every block has and ID, like this:我有一个 json 文件,其中包含 30 条“字典”块,其中每个块都有 ID,如下所示:

{
      "ID": "23926695",
      "webpage_url": "https://.com",
      "logo_url": null,
      "headline": "aewafs",
      "application_deadline": "2020-03-31T23:59:59",
}

Since my script pulls information in the same way from an API more than once, I would like to append new "blocks" to the json file only if the ID doesn't already exist in the JSON file.由于我的脚本不止一次以相同的方式从 API 中提取信息,因此只有当 JSON 文件中不存在 ID 时,我才想将新的“块”附加到 json 文件中。

I've got something like this so far:到目前为止,我有这样的事情:

import os

check_empty = os.stat('pbdb.json').st_size
if check_empty == 0:
    with open('pbdb.json', 'w') as f:
        f.write('[\n]')    # Writes '[' then linebreaks with '\n' and writes ']'
output = json.load(open("pbdb.json"))

for i in jobs:
    output.append({
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    })

with open('pbdb.json', 'w') as job_data_file:
    json.dump(output, job_data_file)

but I would like to only do the "output.append" part if the ID doesn't exist in the Json file.但如果 Json 文件中不存在 ID,我只想执行“output.append”部分。

I am not able to complete the code you provided but I added an example to show how you can achieve the none duplicate list of jobs(hopefully it helps):我无法完成您提供的代码,但我添加了一个示例来展示如何实现不重复的作业列表(希望它有所帮助):

# suppose `data` is you input data with duplicate ids included
data = [{'id': 1, 'name': 'john'}, {'id': 1, 'name': 'mary'}, {'id': 2, 'name': 'george'}]

# using dictionary comprehension you can eliminate the duplicates and finally get the results by calling the `values` method on dict.
noduplicate = list({itm['id']:itm for itm in data}.values())

with open('pbdb.json', 'w') as job_data_file:
    json.dump(noduplicate, job_data_file)

我会和一个数据库的人一起去,谢谢你的时间,我们现在可以关闭这个线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 如果不存在则创建 json 文件,否则追加 - python create json file if it doesn't exist otherwise append 如何在python中忽略嵌套json值的父级(如果不存在)? - How to ignore parent of a nested json value if it doesn't exist, in python? Append 值或如果列表不存在则创建并插入? - Append value or create and insert if the list doesn't exist? python更新列表中的字典或追加,如果不存在 - python update dict in list or append if doesn't exist 如果 python 在 JSON 中找不到特定值,则 append 列表中的内容 - If python doesn't find certain value inside JSON, append something inside list 如果表达式存在,则使用 Jmespath 过滤 JSON 并返回值,如果它不返回 None/Null (python) - Filter JSON using Jmespath and return value if expression exist, if it doesn't return None/Null (python) Android如果不存在python,如何创建一个新的json文件 - Android How to create a new json file if it doesn't exist python Python - 如果 json 数组中不存在密钥,则跳过该密钥 - Python - Skip over key if it doesn't exist in json array Can't append nested JSON value in python pandas dataframe - Can't append nested JSON value in python pandas dataframe Python字典理解检查是否不存在并设置值 - Python dictionary comprehension checking if something doesn't exist and setting a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM