我是JSON的新手。但是我需要知道哪个响应包含NA或缺少值。有谁知道Python中可以使用任何代码来打印未完成的响应。

我有这种回应。下面的示例显示ID 134-bc没有年龄,性别和工作信息。

[{"user_id":"123-ab",
"language":"English",
"demographic_data":{"age":22,"gender":"F","job":4}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}},
{"user_id":"134-bc",
"language":"Chinese",
"demographic_data":{"age":"","gender":"","job":""}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}},
{"user_id":"123-bc",
"language":"Chinese",
"demographic_data":{"age":"21","gender":"M","job":2}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}}
]


输出量
我需要两个单独的文件。

完整的案例放在一个文件中。

[{"user_id":"123-ab",
"language":"English",
"demographic_data":{"age":22,"gender":"F","job":4}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}},
{"user_id":"123-bc",
"language":"Chinese",
"demographic_data":{"age":"21","gender":"M","job":2}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}}
]


另一个文件中不完整的情况可能比该示例更多。

[{"user_id":"134-bc",
"language":"Chinese",
"demographic_data":{"age":"","gender":"","job":""}
"responses":{"Question 1":3,"Question 2":2,"Question 4":1}}
]

最佳答案

在Python字典中,您可以使用innot in测试属性是否完全丢失。如果将空白字段视为“缺少”,则可以使用dict.get(),如果缺少数据,则返回None。

DEFAULT_FIELDS = ["age", "gender", "job"]

def is_missing_data(record, field_names=None):
    field_names = DEFAULT_FIELD_NAMES if field_names is None else field_names
    demographic_data = record["demographic_data"]

    # If "not all" fields are present/truthy, then some data is missing.
    return not all(demographic_data.get(name) for name in field_names)


然后,您可以按以下方式拆分数据:

incomplete_records = [rec for rec in records if is_missing_data(rec)]
complete_records = [rec for rec in records if not is_missing_data(rec)]

with open('incomplete.json', 'w') as f:
    json.dump(incomplete_records, f)

with open('complete.json', 'w') as f:
    json.dump(complete_records, f)

关于python - 是否有任何可用于在JSON中找到完整案例的代码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59606150/

10-14 16:50