python json remove()元素问题一段时间的条件

时间:2017-08-08 16:02:11

标签: python json dictionary

我有一个json文件,格式如下。我需要做的是摆脱整个字典中的" URI"不包含" http"

[
{
    "Exchange Name": "Standard & Poor's Indices",
    "Number": 33.0,
    "URI": "http://us.spindices.com/documents/additional-material/spdji-fixed-income-calendar-2016.xlsx",
    "ref_id": "33_Standard_&_Poor's_Indices"
},
{
    "Exchange Name": "ISE Mercury",
    "Number": 36.0,
    "URI": "follow OPRA",
    "ref_id": "36_ISE_Mercury"
},
{
    "Exchange Name": "Aequitas Neo",
    "Number": 37.0,
    "URI": "email for holidays",
    "ref_id": "37_Aequitas_Neo"
},
{
    "Exchange Name": "FINRA SPDS 144A",
    "Number": 38.0,
    "URI": "https://www.finra.org/industry/trace/trace-holiday-calendar",
    "ref_id": "38_FINRA_SPDS_144A"
}
]

到目前为止,我一直坚持使用以下功能。这里的问题是remove()实际上并没有删除一个' URI'字符串中的元素。但是在我第二次运行代码之后,它才有效。我想我需要使用while循环,但是如何在此设置中实现它。

def sys_validate_data():
    with open('./data_out/uniq_set.json') as jf:
        json_decoded = json.load(jf)
        for ix in json_decoded:
            if "http" not in ix["URI"]:
                json_decoded.remove(ix)

        with open('./data_out/uniq_set.json', 'w') as fpw:
            json.dump(list(json_decoded), fpw, sort_keys=True, indent=4)

2 个答案:

答案 0 :(得分:2)

在迭代时不要修改列表。这样做会产生意外行为。相反,您可以使用列表推导来过滤JSON列表中的元素:

def sys_validate_data():
    with open('./data_out/uniq_set.json') as jf:
        json_decoded = [ix for ix in json.load(jf) if "http" in ix["URI"]]
        ....

答案 1 :(得分:1)

使用列表理解,在迭代时不要改变列表:

validated_json = [entry for entry in json_decoded if entry['URI'].startswith('http')]

扩展示例:

def sys_validate_data():
    with open('./data_out/uniq_set.json') as jf:
        json_decoded = json.load(jf)
        validated_json = [entry for entry in json_decoded if entry['URI'].startswith('http')]

        with open('./data_out/uniq_set.json', 'w') as fpw:
            json.dump(validated_json, fpw, sort_keys=True, indent=4)