0
我的Python代碼是這樣使用readlines方法
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
閱讀第一N行但是,當我做到這一點k是一個數組的所有生產線從file.txt的
我試圖找出閱讀閱讀只有第一n
線,並使用np.asarray
店k
如何與n
感謝您的幫助編輯該代碼!
我的Python代碼是這樣使用readlines方法
with open('file.txt') as w:
k = np.asarray(w.readlines(),np.float)
閱讀第一N行但是,當我做到這一點k是一個數組的所有生產線從file.txt的
我試圖找出閱讀閱讀只有第一n
線,並使用np.asarray
店k
如何與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]
你試過'readlines方法():N]'? –
它的工作感謝人!傻我 –
警告'[:n]'不適用於大文件(文件必須複製到內存中)。第二種方法是首選 –