Python JSON文件读取生成器(多行)

我有一堆带有多行的json文件,如下所示:

文件1

{"id":1,"name":"Eric","height":1.80, ...},
{"id":2,"name":"Bob","height":1.90, ...}   
...

文件2

{"id":3,"name":"Jenny","height":1.50, ...},
{"id":4,"name":"Marlene","height":1.60, ...} 
...

我想构建一个生成器以将每一行作为字典生成。我当前的代码:

from typing import Iterator, Dict, Any, Optional
import io
import os

def json_gen2(file_list: list[str]) -> Iterator[Dict[str, Any]]:
    import json
    for file in file_list:    
        with open(file) as json_file: 
            data = []
            for line in json_file:
                data = json.load(line)
                if not data:
                    break
                yield data

datapath = os.path.normcase(os.getcwd()) + '/data/log_data'
file_list = get_files(datapath) # create path list of json files
jsonfile = json_gen2(file_list)
next(jsonfile)    

i get the following Error Message

请帮助:)

评论