Read only a specific line from a json file

49 Views Asked by At

I have a large json file containing +8 million lines. Due to memory restriction i'm trying to find how to only read a specific line from the file without load the whole file into memory.

What i'm looking for is function that basically behaves like this:

def read_line(file_name, line_number)
 return the line line_number from the file file_name
2

There are 2 best solutions below

3
Shrikant Shejwal_IND On

Here is what you are looking for-

import linecache

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

line_number = 2
file_name = 'sample-json-file.json'
line = read_line(file_name, line_number)
print(line)
2
Lorenzo P On

If you don't care about cache, you can loop on the file using a context manager.

def read_line(file_path: str, line_num: int) -> str:
    with open(file_path, 'r') as file:
        for number, line in enumerate(file):
            if number == line_num:
                return line