简体   繁体   中英

How can I open multiple json files in Python with a for loop?

For a data challenge at school we need to open a lot of json files with python. There are too many to open manually. Is there a way to open them with a for loop?

This is the way I open one of the json files and make it a dataframe (it works).

file_2016091718 = '/Users/thijseekelaar/Downloads/airlines_complete/airlines-1474121577751.json'

json_2016091718 = pd.read_json(file_2016091718, lines=True)

Here is a screenshot of how the map where the data is in looks (click here)

Yes, you can use os.listdir to list all the json files in your directory, create the full path for all of them and use the full path using os.path.join to open the json file

import os
import pandas as pd
base_dir = '/Users/thijseekelaar/Downloads/airlines_complete'

#Get all files in the directory

data_list = []
for file in os.listdir(base_dir):

    #If file is a json, construct it's full path and open it, append all json data to list
    if 'json' in file:
        json_path = os.path.join(base_dir, file)
        json_data = pd.read_json(json_path, lines=True)
        data_list.append(json_data)

print(data_list)

Try this :

import os

# not sure about the order
for root, subdirs, files in os.walk('your/json/dir/'):
    for file in files:
        with open(file, 'r'):
            #your stuff here

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