今天直接面對(duì)了一個(gè)很嚴(yán)重的問題,生成的一個(gè)大的字典,如何打開成了一個(gè)問題,因此發(fā)覺了一下這個(gè)問題:
基本原理:通過不斷取1024KB為大小的內(nèi)容,然后通過迭代器yield這個(gè)設(shè)備讀取無(wú)限大的內(nèi)容。
#coding=utf-8import os.pathimport time'''切分成小文件,然后讀取'''def read_in_block(filepath): BLOCK_SIZE = 1024 with open(filepath,'r') as f: while True: block = f.read(BLOCK_SIZE) if block: yield block else: returnif __name__ == '__main__': path=os.getcwd() filepath = path + '/pass.txt' start_time = time.time() for i in read_in_block(filepath): PRint i end_time = time.time() print 'Time is ',end_time - start_time新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注