skip to Main Content

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

Answers


  1. 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)
    
    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search