简体   繁体   English

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

[英]Python JSON-file-reading generator (multi-line)

I have a bunch of json files with multiple lines who look like this:我有一堆 json 文件,多行看起来像这样:

file1文件 1

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

file2文件2

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

I want to build a generator to yield each line as a dictionary.我想构建一个生成器以将每一行作为字典产生。 My current code:我当前的代码:

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

def json_gen2(file_list: list) -> 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我收到以下错误消息

pls help:)请帮助:)

Oops, I misread.糟糕,我看错了。 You are doing the same thing I was saying.你做的和我说的一样。 Your error is due to using 'load' instead of 'loads'.您的错误是由于使用“加载”而不是“加载”。 Each line returned by返回的每一行

for line in json_file:
    data = json.load(line)

is a string, and you're attempting to read it as a file pointer.是一个字符串,您正试图将其作为文件指针读取。

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

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