方法一:執行時間較短,但耗費記憶體 (readlines)f = open(filename,'r') for line in f.readlines(): if condition: do something f.close()方法二:省記憶體,執行時間較久 - 適合開大檔
f = open(filename,'r')
for line in f:
if condition:
do something
f.close()
下面的圖轉自 - Python: speed vs. memory tradeoff reading files - isilanes on February 15, 2008
Fig. 1: Memory vs file size for both methods of reading the file
Fig. 2: Execution time vs file size for both methods of reading the file
請先 登入 以發表留言。