Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从JSON文件中删除空值和假值?_Python_Json_Python 3.x - Fatal编程技术网

Python 如何从JSON文件中删除空值和假值?

Python 如何从JSON文件中删除空值和假值?,python,json,python-3.x,Python,Json,Python 3.x,我有一个包含这些数据的JSON文件 { "in_reply_to_screen_name": null, "favorited": false, "id_str": "92", "entities": { "user_mentions": [], "symbols": [], "urls": [], "hashtags": [ { "indic

我有一个包含这些数据的JSON文件

{

    "in_reply_to_screen_name": null,
    "favorited": false,
    "id_str": "92",
    "entities": {
        "user_mentions": [],
        "symbols": [],
        "urls": [],
        "hashtags": [
            {
                "indices": [0,8]
            }
        ]
    },
    "geo": null,
    "user": {
        "verified": false,
        "notifications": null,
        "profile_sidebar_border_color": "FFFFFF",
        "geo_enabled": true,
        "profile_background_tile": true,
        "url": null,
        "id": 278,
        "default_profile": false,
        "lang": "pt",
        "location": null,
        "translator_type": "none",
        "protected": false
    },
    "id": 92,
    "in_reply_to_status_id_str": null,
    "in_reply_to_status_id": null,
    "created_at": "Tue Oct",
    "is_quote_status": false,
    "text": "This is a vdd",
    "truncated": false,
    "retweeted": false
}
如何使用Python删除包含该文件null、false和true的任何键值对


这些值可以出现在数据结构的不同级别。

通过解码、递归处理对象并再次编码为JSON

我喜欢使用以下命令执行此类任务:

from functools import singledispatch

@singledispatch
def remove_null_bool(ob):
    return ob

@remove_null_bool.register(list)
def _process_list(ob):
    return [remove_null_bool(v) for v in ob]

@remove_null_bool.register(dict)
def _process_list(ob):
    return {k: remove_null_bool(v) for k, v in ob.items()
            if v is not None and v is not True and v is not False}

data = json.load(source)
json.dump(dest, remove_null_bool(data))
我使用了
不为假
等来测试精确的对象。如果我在{None,False,True}中使用了
v not,整数值
0
1
也将被删除,因为
False
True
分别等于这些值

针对加载到
数据中的样本进行演示:

>>> print(json.dumps(remove_null_bool(data), indent=4, sort_keys=True))
{
    "created_at": "Tue Oct",
    "entities": {
        "hashtags": [
            {
                "indices": [
                    0,
                    8
                ]
            }
        ],
        "symbols": [],
        "urls": [],
        "user_mentions": []
    },
    "id": 92,
    "id_str": "92",
    "text": "This is a vdd",
    "user": {
        "id": 278,
        "lang": "pt",
        "profile_sidebar_border_color": "FFFFFF",
        "translator_type": "none"
    }
}

奇怪的是,标题中提到了“null,false”(即“falsy”)值,但问题文本却说“null,false和true”。是哪一个?@KevinJohnson,这些步骤例如:在f:tweet=json.loads(line)中用open(arq_geral,'r')作为f:for行dados_tweet。为dados_tweet中的tweet添加(tweet):如果tweet[“geo”]:del tweet[“geo”]它不会删除“geo”,例如。我在标题中的错误@Alexis运行时,它无法识别“dest”。NameError:如果此程序中发生这种情况,是否未定义名称“dest”?还是我的错?file=open('file_ok.json','w')#file来写入正确的数据文件_data='file_data.json'#json文件,具有null、false和true值,open(file_data,'r')作为f:for行在f:data=json.load(line)文件中。write(json.dump(dest,remove_null_bool(data)))文件。write('\n')file.close()@Rennan,你认为dest的
dest
来自哪里?看看代码,查找
json.dump()
,你就可以回答你自己的问题了。@RennanCéosGleyson,我正在举例说明;提供您自己的输入和输出文件对象。