仅读取 json 文件中的特定行

问题描述 投票:0回答:1

我有一个包含超过 800 万行的大型 json 文件。由于内存限制,我试图找到如何仅从文件中读取特定行而不将整个文件加载到内存中。

我正在寻找的功能基本上是这样的:

def read_line(file_name, line_number)
 return the line line_number from the file file_name
python json file
1个回答
0
投票

这就是您要找的东西-

import linecache

def read_line(file_name, line_number):
    return linecache.getline(file_name, line_number)

line_number = 100 
file_name = 'sample-json-file.json'
line = read_line(file_name, 2)
print(line)
© www.soinside.com 2019 - 2024. All rights reserved.