1

My python code goes like this

with open('file.txt') as w:
    k = np.asarray(w.readlines(),np.float)

But when I do this k is an array with all lines read from file.txt

I am trying to figure to read only the first n lines and store k using np.asarray How to edit this code with n

Thanks for the help!

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
papaya
  • 1,455
  • 1
  • 12
  • 24

1 Answers1

2
from itertools import islice
with open("file.txt") as myfile:
    k = list(islice(myfile, n))
print k

or

with open('file.txt') as w:
    k = np.asarray(w.readlines(),np.float)
    k = k[:,n]
Sunil B N
  • 4,001
  • 1
  • 30
  • 49