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):
List.append(sys.stdin.readline().strip())
if __name__ == "__main__":
n = int(sys.stdin.readline().strip())
ReadNLines(n)
n = int(sys.stdin.readline().strip())
ReadNLines(n)
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)]
print(head)
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))
print(head)
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:
try:
while True:
data = islice(fin, 0, 8)
firstname = next(data)
lastname = next(data)
email = next(data)
#.....
except StopIteration:
pass
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:
try:
while True:
data = islice(fin, 0, 3)
data = record(*data)
print data.firstname, data.lastname, data.email #.......
except (StopIteration, TypeError):
pass
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")
print(lastline)
print(line)
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
Python Script Execute Commands in Terminal
Django-Registration & Django-Profile, Using Your Own Custom Form
Attributeerror: 'List' Object Has No Attribute 'Click' - Selenium Webdriver
How to Create an SQL View with SQLalchemy
Redirect While Passing Arguments
Python: Sort Function Breaks in the Presence of Nan
How to Schedule a Function to Run Every Hour on Flask
How to Find Tags with Only Certain Attributes - Beautifulsoup
Minimum Euclidean Distance Between Points in Two Different Numpy Arrays, Not Within
Convert Bytes to Bits in Python
Iterate Over All Combinations of Values in Multiple Lists in Python
Using Django Database Layer Outside of Django
Why Are Default Arguments Evaluated at Definition Time
How to Print Utf-8 Encoded Text to the Console in Python < 3