Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time efficient way to skip no of line from very large text file (16gb) using python

I have a very large text file of 16gb . I need to skip no of line .I want to skip those line in time efficient manner. I am using python for code.how to do that ?

like image 213
Abhi Avatar asked Jul 04 '20 02:07

Abhi


People also ask

How do you skip a certain number of lines in Python?

There are many ways in which you can skip a line in python. Some methods are: if, continue, break, pass, readlines(), and slicing.

How do I read a 100gb file in Python?

Reading Large Text Files in Python We can use the file object as an iterator. The iterator will return each line one by one, which can be processed. This will not read the whole file into memory and it's suitable to read large files in Python.


2 Answers

Just read the number of lines you want to skip and throw them away:

with open(your_file) as f_in:
    for i in range(number_of_lines_to_skip):
        f_in.readline()
    # your file is now at the line you want...  

You can also use enumerate to have a generator that only yields lines once you have skipped the lines you want to:

with open(your_file) as f_in:
    for line in (line for i, line in enumerate(f_in) if i>lines_to_skip):
        # here only when you have skipped the first lines

The second there is likely faster.

like image 68
dawg Avatar answered Nov 06 '22 20:11

dawg


beware, calling next on a file object will raise StopIteration if the end of file is reached.

go_to_line_number = some_line_number

with open(very_large_file) as fp:

    for _ in range(go_to_line_number):
        next(fp)

    for line in fp:
        # start your work from desired line number
        pass
like image 34
Vishal Singh Avatar answered Nov 06 '22 20:11

Vishal Singh