简体   繁体   English

Python更新JSON文件

[英]Python to update JSON file

After googling around and checking some other posts I still haven't found a solution for my issue. 在四处搜寻并检查了一些其他帖子之后,我仍然没有找到解决我问题的方法。

Let me quickly explain what I'm after: 让我快速解释一下我要做什么:

I've got a JSON configuration file with the following syntax: 我有一个使用以下语法的JSON配置文件:

    [
        {
                "name": "Name 1",
                "provider": "Provider 1",
                "url": "/1",
                "source": "URL"
        },
        {
                "name": "Name 2",
                "provider": "Provider 2",
                "url": "/1",
                "source": "URL 2"
        }

]

My issue is the key source changes and I've got a script generating these however I'm not able to find a way to update them automatically ideally I would need to tell my python script to look out for "Provider 1" and then update with the new URL for Provider 1 but my JSON format does not include individual keys and hence I need to search via Provider name and update the source. 我的问题是关键源更改,并且我有一个脚本来生成这些更改,但是理想情况下我无法找到一种自动更新它们的方法,我需要告诉我的python脚本注意“ Provider 1”,然后进行更新提供商1的新URL,但我的JSON格式不包含单个键,因此我需要通过提供商名称进行搜索并更新源。

Hope this makes sense any help appreciated. 希望这对任何帮助表示感激。

You can try something similar to below; 您可以尝试以下类似的方法;

    myList = [
            {
                    "name": "Name 1",
                    "provider": "Provider 1",
                    "url": "/1",
                    "source": "URL"
            },
            {
                    "name": "Name 2",
                    "provider": "Provider 2",
                    "url": "/1",
                    "source": "URL 2"
            }

    ]

    def insertOrUpdate(updatedObj):
            existing = [x for x in myList if x['provider'] == updatedObj['provider']]
            if len(existing) == 1:
                    existing[0]['url'] = updatedObj['url']
            else:
                    myList.append(updatedObj)

    insertOrUpdate({"name": "Name 1", "provider": "Provider 1", "url": "updated Url", "source": "URL"})
    insertOrUpdate({"name": "Name X", "provider": "Provider X", "url": "new Url", "source": "URL"})

Assuming test.json is your json file containing the text you inserted in the question, you can do the following 假设test.json是包含您在问题中插入的文本的json文件,则可以执行以下操作

import json
with open('test.json') as my_file:
    json_content = json.load(my_file)
for ele in json_content:
    print(ele)

{'name': 'Name 1', 'provider': 'Provider 1', 'source': 'URL', 'url': '/1'} {'name':'Name 1','provider':'Provider 1','source':'URL','url':'/ 1'}

{'name': 'Name 2', 'provider': 'Provider 2', 'source': 'URL 2', 'url': '/1'} {'name':'Name 2','provider':'Provider 2','source':'URL 2','url':'/ 1'}

new_url = 'something. Define it as you want.'
for ele in json_content:
    if ele['provider']=='Provider 1':
        ele['url']=new_url
for ele in json_content:
    print(ele)
for ele in json_content:

print(ele)

{'name': 'Name 1', 'provider': 'Provider 1', 'source': 'URL', 'url': 'something. {'name':'Name 1','provider':'Provider 1','source':'URL','url':'something。 Define it as you want.'} {'name': 'Name 2', 'provider': 'Provider 2', 'source': 'URL 2', 'url': '/1'} 根据需要定义它。'} {'name':'Name 2','provider':'Provider 2','source':'URL 2','url':'/ 1'}

Thank you very much both answers do work I just need to adapt some more code in order to make it ready as @MMF suggested the script should then run via cron. 非常感谢两个答案都起作用,我只需要改编更多代码即可使其准备就绪,因为@MMF建议脚本随后应通过cron运行。

Thanks a lot for your help guys!! 非常感谢您的帮助!!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM