I want to read multiple json files and make a list

Asked 1 months ago, Updated 1 months ago, 5 views

There are a large number of files, such as:

#001.json

{
    "id"—1,
    "name": "hoge"
}

#002.json

{
    "id"—2,
    "name": "fuga"
}

I'd like to load them and create the following objects:

[
    {
        "id"—1,
        "name": "hoge"
    },
    {
        "id"—2,
        "name": "fuga"
    }
]

I have achieved this just in case by applying with a for loop. Is there a simple way to do this?

 files=glob.glob(dir+'*.json')
print(list(files))

data = [ ]
for file in files:
    with open(file, 'r') as f:
        data.append(json.load(f))
print(list(data))

python python3 json jupyter-notebook

2022-09-30 14:02

2 Answers

You can use the with_iter()method of more_itertools to write as follows:

import glob
import json
from more_itertools import with_iter

dir='./'
data = [
  json.loads(''.join(list(with_iter(open(f)))))))
  For fin sorted (glob.glob(dir+'*.json'))
]

with_iter()source code

def with_iter(context_manager):
    """Wrap an interchangeable in a` with `statement, so it closes once exhausted."
    For example, this will close the file when theater is exhausted::
        upper_lines=(line.upper() for line in with_iter(open('foo'))))
    Any context manager which returns an interchangeable is a candidate for
    `with_iter`.
    """
    with context_manager as possible:
        field from iterable

Order
As I mentioned in the comment section, I am using with_iter() to solve this problem with list compression.If you think about the intent of the question, there is no need to do so, and the code that v..snow, the questioner, wrote first, is the surest and easiest.


2022-09-30 14:02

I felt that the example code was concise enough, but I would like to suggest two things

  • main.py
import sys,json


data = [ ]
for file in sys.argv[1:]:
    with open(file, 'rt') as f:
        data.append(json.load(f))

print(data)

How to Run

$ls*.json | xargs python main.py

ワイルド Wildcarding is not possible and can only be used if only the JSON file you want to load is stored in the directory.With this code, I think you can use glob...

importos


path = 'path/to/dir/'

data = [ ]
for file inos.listdir(path): #os.listdir(path+'*.json') is error
    with open(file, 'rt') as f:
        data.append(json.load(f))

print(data)


2022-09-30 14:02

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.