使用 readlines 读取前 N 行
Read first N lines using readlines
我的 python 代码是这样的
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
但是当我这样做时,k 是一个数组,其中包含从 file.txt
读取的所有行
我试图计算只读取前 n
行并使用 np.asarray
存储 k
How to edit this code with n
感谢您的帮助!
from itertools import islice
with open("file.txt") as myfile:
k = list(islice(myfile, n))
print k
或
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
k = k[:,n]
我的 python 代码是这样的
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
但是当我这样做时,k 是一个数组,其中包含从 file.txt
读取的所有行我试图计算只读取前 n
行并使用 np.asarray
存储 k
How to edit this code with n
感谢您的帮助!
from itertools import islice
with open("file.txt") as myfile:
k = list(islice(myfile, n))
print k
或
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
k = k[:,n]