Omaxwellok Omaxwellok
  • 15-04-2020
  • Computers and Technology
contestada

Write a program that Read first n lines of file

Respuesta :

starmagpro
starmagpro starmagpro
  • 15-04-2020

Answer:

Python 2:

with open("datafile") as myfile:

   head = [next(myfile) for x in xrange(N)]

print head

Python 3:

with open("datafile") as myfile:

   head = [next(myfile) for x in range(N)]

print(head)

Both Python 2 & 3:

from itertools import islice

with open("datafile") as myfile:

   head = list(islice(myfile, N))

print head

Answer Link

Otras preguntas

How many ways can you choose a set of 9 pencils from a selection of 10?
What proportion of the first 10,000 natural numbers contain a 3
A pencil is dropped from the roof of the school what is the speed after 3s down is the negative direction. I'm confused on what equation to use
how to change 50/12 to a mixed number?
6. Minimum value determined by the formula function f (x) = 2x ²-8x + p was 20. Value f (2) is.7. Shape factor of the quadratic equation 4x ²-13x = -3 is ...8.
how many tenths are in 15.03
how do i say have a nice day in Spanish
How long will lucy have to wait before her $2500 invested at 6% earns $600 ?
The sum of two numbers is 30 and their difference is 2.  Find the two numbers by writing and solving a system of equations
what is a 'light at the end of the tunnel'?