简体   繁体   English

如何将存储在文件夹中的多个 json 文件读取到 Python 中的不同字典中?

[英]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.假设一个文件夹中有 20 个 JSON 文件,我想在 python 程序中将它们读取并存储到 20 个不同的字典中。

You may use a list to hold the filenames, then apply the dict reading for each您可以使用list来保存文件名,然后为每个应用读取

import json
from pathlib import Path

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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