Python从所有子目录读取JSON文件 [英] Python read JSON files from all sub-directories

查看:850
本文介绍了Python从所有子目录读取JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下文件夹结构:

I have a following folder structure:

Directory    
    - Subdirectory 1:
       file.json
    - Subdirectory 2:
       file.json
    - Subdirectory 3:
       file.json
    - Subdirectory 4:
       file.json

如何使用Pandas读取这些JSON文件?

How do I read these JSON files using Pandas?

推荐答案

您可以执行以下操作:

import glob, os
working_directory = os.getcwd()

sub_directories = [active_directory + "/" + x for x in os.listdir(working_directory) if os.path.isdir(active_directory + "/"+x)]
all_json_files = []

for sub_dir in sub_directories:
    os.chdir(sub_dir)
    for file in glob.glob("*.json"):
        all_json_files.append(sub_dir + "/" + file)

#Get back to original working directory
os.chdir(working_directory)

list_of_dfs = [pd.read_json(x) for x in all_json_files]

从那里开始,如果所有json文件都具有相同的结构,则可以将它们连接起来以得到一个单个数据帧:

From there, if all json files have the same structure, you could concatenate them to get one single dataframe:

final_df = pd.concat(list_of_dfs)

这篇关于Python从所有子目录读取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆