Python 读取大文件方法

一般通过with open('d:/abalone.txt','r') as f: f.read()这种方法一般对小文件合适,但对于大文件需要分块处理,方法主要有:

(1)一行一行读,自己通过程序控制行的数据,然后进行处理。

with open('d:/abalone.txt','r') as f:    
    while  True:
        line=f.readline()
        print(line)
        if not line:

             break

(2)一块一块读,道理同上面。

blocksize=16   #define block,根据实际调整
with open('d:/abalone.txt','r') as f:    
    while  True:
        block=f.read(blocksize)
        if not block:
             break


猜你喜欢

转载自blog.csdn.net/weixin_42039090/article/details/80504336