简体   繁体   中英

How can I read multiple in json files in in folder structure?

I'm trying to read Training data for machine learning in json files, but they are stored in nested folders.

在此处输入图像描述

I'd like to know how can I read each json file into pandas frame.

Imagine there are three JSON files in a nested folder under data folder.

$ tree data
data
├── date1
│   ├── date2
│   │   └── file1_date2.json
│   └── file1.json
└── file1.json

glob2 module can be used to fetch the JSON files recursively. glob returns a list of files.

from glob2 import glob
jsonFiles = glob('data/**/*.json') #Can be used absolute or relative paths
print(jsonFiles)
['data/file1.json',  'data/date1/file1.json',  'data/date1/date2/file1_date2.json']

JSON files ban be loaded into dataframe by iterating thru the list jsonFiles .

dfList = []
for jsonFile in jsonFiles:
    df = pd.read_json(jsonFile)
    dfList.append(df)
    
dfTrainingDF = pd.concat(dfList, axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM