简体   繁体   中英

How to read multiple json files stored in a folder into different dictionaries in Python?

Suppose there are 20 JSON files in a folder and I want to read & store them into 20 different dictionaries in a python program.

You may use a list to hold the filenames, then apply the dict reading for each

import json
from pathlib import Path

files = ['a.txt', 'b.txt', 'c.txt']  # ...
dicts = [json.loads(Path(file).read_text()) for file in files]

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