Python How to Read N Number of Lines at a Time

How to read file N lines at a time?

One solution would be a list comprehension and the slice operator:

with open(filename, 'r') as infile:
lines = [line for line in infile][:N]

After this lines is tuple of lines. However, this would load the complete file into memory. If you don't want this (i.e. if the file could be really large) there is another solution using a generator expression and islice from the itertools package:

from itertools import islice
with open(filename, 'r') as infile:
lines_gen = islice(infile, N)

lines_gen is a generator object, that gives you each line of the file and can be used in a loop like this:

for line in lines_gen:
print line

Both solutions give you up to N lines (or fewer, if the file doesn't have that much).

How to read n lines at a time from stdin?

You need to remove the newline that sys.stdin.readline() includes in the result. And you need to convert n to an integer.

import sys
def ReadNLines(n):
List =[]
for _ in range(n):

if __name__ == "__main__":
n = int(sys.stdin.readline().strip())
n = int(sys.stdin.readline().strip())

Since you never use the line variable, the convention is to use _ as a dummy variable. You can also convert the function into a list comprehension:

def readNLines(n):
return [sys.stdin.readline().strip() for _ in range(n)]

How to read first N lines of a file?

Python 3:

with open("datafile") as myfile:
head = [next(myfile) for x in range(N)]

Python 2:

with open("datafile") as myfile:
head = [next(myfile) for x in xrange(N)]
print head

Here's another way (both Python 2 & 3):

from itertools import islice

with open("datafile") as myfile:
head = list(islice(myfile, N))

Read a File 8 Lines at a Time Python

A simple implementation using itertools.islice

from itertools import islice
with open("test.txt") as fin:
while True:
data = islice(fin, 0, 8)

firstname = next(data)
lastname = next(data)
email = next(data)
except StopIteration:

A better more pythonic implementation

>>> from collections import namedtuple
>>> from itertools import islice
>>> records = namedtuple('record',
('firstname','lastname','email' #, .....
>>> with open("test.txt") as fin:
while True:
data = islice(fin, 0, 3)

data = record(*data)
print data.firstname, data.lastname, #.......
except (StopIteration, TypeError):

Read two lines at a time from a txt file

you can iterate over the file using enumerate to get line numbers, and simply store even number lines in a temp variable, and move on to the next odd number line. On odd number lines you can then have access to the previous line as well as the current line.

with open('somefile.txt', 'r') as f:
lastline = ''
for line_no, line in enumerate(f):
if line_no % 2 == 0: #even number lines (0, 2, 4 ...) go to `lastline`
lastline = line
continue #jump back to the loop for the next line
print("here's two lines for ya")

How do I read two lines from a file at a time using python

Similar question here. You can't mix iteration and readline so you need to use one or the other.

while True:
line1 = f.readline()
line2 = f.readline()
if not line2: break # EOF

Related Topics

Leave a reply