如何在python中将多个json文件(来自网站的元数据)合并到一个json文件或EDA中?

2024-06-16 11:10:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理2个文件夹的混合json文件和收集的音频(大约400个json文件),如下所示:

{
  "id":"c79c32e7-6665-4c5e-9458-d15930488263",
  "age":"34",
  "gender":"m",
  "healthStatus":"healthy",
  "audioFiles":[
    "1585940317337_sentence_healthy_m_34_c79c32e7-6665-4c5e-9458d15930488263.wav",
    "1585940317337_cough_healthy_m_34_c79c32e7-6665-4c5e-9458-d15930488263.wav",
    "1585940317337_breath_healthy_m_34_c79c32e7-6665-4c5e-9458d15930488263.wav"
  ]
}

我想检索agegenderhealthStatus并将它们合并到一个JSON文件中,以便在python中进行分析

为此,我写道:

from pathlib import Path
import json
data_folder = Path("/Users/jiani/Desktop/Voicemed/#ml/cough_classification-original_experiment/new_data/meta1")
read_files = glob.glob("data_folder/.json")

output_list = []

for f in read_files:
    with open(f, "rb") as infile:
        output_list.append(json.load(infile))

with open("merged_file.json", "w") as outfile:
    json.dump(output_list, outfile)

然后我打印了output_list,但是我得到了一个空的。我已经读了一些相关的解决方案,但我仍然无法得到答案。有人能帮我吗

多谢各位


Tags: 文件pathimportjsonoutputagedatagender
1条回答
网友
1楼 · 发布于 2024-06-16 11:10:49

试试这个:

from os import listdir
from os.path import isfile, join
import json

data_folder = "full/path/to/jsons"
files = [join(data_folder,f) for f in listdir(data_folder) if isfile(join(data_folder, f)) and f.endswith(".json")]

output_list = []
for file in files:
    with open(file, "r") as f:
        output_list.append({k:v for k,v in json.load(f).items() if k in ["age","gender","healthStatus"]})

with open("merged_file.json", "w") as outfile:
    json.dump(output_list, outfile)

相关问题 更多 >