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

Karen runs a print shop that makes posters for large companies. It is a very competitive business. The market price is currently $1.00 per poster. She has fixed
3 less than twice a number q
why didnt the working class support the nazi party
Suppose you begin with a pile of n stones and split this pile into n piles of one stone each by successively splitting a pile of stones into two smaller piles.
What is 8(4k-4)=-5k-32?
What is the proper name is the quadrilateral below
What caused the downfall of Ghana? A. Climate change and Berber battles B. Songhai invasions and disease C. Internal revolts D. Depletion of the salt mines
Does the PLANT CELL have CHLOROPLASTS? A. Yes B. No
Which of the following is a perceptual region?
It takes 73 days for an egg of a bearded dragon lizard to hatch. what fraction in simplest form represents the part of a year, in days, it takes for the egg to