亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將多個 json 文件合并到一個 json 文件 dor EDA 在 python 中?

如何將多個 json 文件合并到一個 json 文件 dor EDA 在 python 中?

喵喵時光機 2022-10-05 09:35:45
我正在處理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"  ]}我想檢索 ,并將它們合并到一個JSON文件中,以便在python中進行分析。agegenderhealthStatus為此,我寫道:from pathlib import Pathimport jsondata_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
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

試試這個:


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)


查看完整回答
反對 回復 2022-10-05
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號