Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read lines by number from a large file

Tags:

I have a file with 15 million lines (will not fit in memory). I also have a small vector of line numbers - the lines that I want to extract.

How can I read-out the lines in one pass?

I was hoping for a C function that does it on one pass.

like image 767
Aleksandr Levchuk Avatar asked Aug 23 '11 05:08

Aleksandr Levchuk


People also ask

How do I read 10 lines from a file in Python?

Use readlines() to Read the range of line from the File The readlines() method reads all lines from a file and stores it in a list. You can use an index number as a line number to extract a set of lines from it. This is the most straightforward way to read a specific line from a file in Python.

How do I read large files?

To be able to open such large CSV files, you need to download and use a third-party application. If all you want is to view such files, then Large Text File Viewer is the best choice for you. For actually editing them, you can try a feature-rich text editor like Emacs, or go for a premium tool like CSV Explorer.

How do you iterate through a large file in Python?

use of with with is the nice and efficient pythonic way to read large files. advantages - 1) file object is automatically closed after exiting from with execution block. 2) exception handling inside the with block. 3) memory for loop iterates through the f file object line by line.


1 Answers

The trick is to use connection AND open it before read.table:

con<-file('filename') open(con)  read.table(con,skip=5,nrow=1) #6-th line read.table(con,skip=20,nrow=1) #27-th line ... close(con) 

You may also try scan, it is faster and gives more control.

like image 136
mbq Avatar answered Oct 30 '22 21:10

mbq