根据条件替换Json字符串中的信息 [英] replace information in Json string based on a condition

查看:77
本文介绍了根据条件替换Json字符串中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个嵌套键的非常大的json文件.到目前为止,如果您愿意的话,我已经读过了:

I have a very large json file with several nested keys. From whaat I've read so far, if you do:

x = json.loads(data)

Python会将其解释为字典(如果我错了,请纠正我). json文件中的第四层嵌套包含几个用ID号命名的元素,所有这些元素都包含一个称为子元素的元素,如下所示:

Python will interpret it as a dictionary (correct me if I'm wrong). The fourth level of nesting in the json file contains several elements named by an ID number and all of them contain an element called children, something like this:

{"level1":
    {"level2":
        {"level3":
            {"ID1":
                {"children": [1,2,3,4,5]}
            }
            {"ID2":
                {"children": []}
            }
            {"ID3":
                {"children": [6,7,8,9,10]}
            }
      }
   }
}

我需要做的是用任何内容替换所有"children"元素中的所有项目,如果ID号在名为new_ids的列表中,则表示"children": [],然后将其转换回json.我已经在这个主题上阅读了几个小时,但是我没有找到类似的东西来帮助自己.

What I need to do is to replace all items in all the "children" elements with nothing, meaning "children": [] if the ID number is in a list called new_ids and then convert it back to json. I've been reading on the subject for a few hours now but I haven't found anything similar to this to try to help myself.

我正在运行Python 3.3.3.任何想法都将不胜感激!

I'm running Python 3.3.3. Any ideas are greatly appreciated!!

谢谢!

编辑

列表:

new_ids=["ID1","ID3"]

预期结果:

{"level1":
    {"level2":
        {"level3":
            {"ID1":
                {"children": []}
            }
            {"ID2":
                {"children": []}
            }
            {"ID3":
                {"children": []}
            }
      }
   }
}

推荐答案

首先,您的JSON无效.我想你想要这个:

First of all, your JSON is invalid. I assume you want this:

{"level1":
    {"level2":
        {"level3":
            {
            "ID1":{"children": [1,2,3,4,5]},
            "ID2":{"children": []},
            "ID3":{"children": [6,7,8,9,10]}
            }
        }
    }
}

现在,将数据作为字典加载:

Now, load your data as a dictionary:

>>> with open('file', 'r') as f:
...     x = json.load(f)
... 
>>> x
{u'level1': {u'level2': {u'level3': {u'ID2': {u'children': []}, u'ID3': {u'children': [6, 7, 8, 9, 10]}, u'ID1': {u'children': [1, 2, 3, 4, 5]}}}}}

现在,您可以遍历x['level1']['level2']['level3']中的键,并检查它们是否在您的new_ids中.

Now you can loop over the keys in x['level1']['level2']['level3'] and check whether they are in your new_ids.

>>> new_ids=["ID1","ID3"]
>>> for key in x['level1']['level2']['level3']:
...     if key in new_ids:
...         x['level1']['level2']['level3'][key]['children'] = []
... 
>>> x
{u'level1': {u'level2': {u'level3': {u'ID2': {u'children': []}, u'ID3': {u'children': []}, u'ID1': {u'children': []}}}}}

您现在可以将x写回到这样的文件中:

You can now write x back to a file like this:

with open('myfile', 'w') as f:
    f.write(json.dumps(x))

如果您的new_ids列表很大,请考虑将其设为set.

If your new_ids list is large, consider making it a set.

这篇关于根据条件替换Json字符串中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆