且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从大文件中读取行分隔的JSON(逐行)

更新时间:2023-08-26 18:27:10

此时只需阅读每一行并构造一个json对象:

Just read each line and construct a json object at this time:

with open(file_path) as f:
    for line in f:
        j_content = json.loads(line)

这样,您可以加载正确的完整json对象(假设json对象中某处或中间的json值中没有\n),并且可以避免在需要时创建每个对象的内存问题.

This way, you load proper complete json object (provided there is no \n in a json value somewhere or in the middle of your json object) and you avoid memory issue as each object is created when needed.

也有这个答案.

https://***.com/a/7795029/671543