python逐行读入大文件

对于小文件,可以用readlines()

f = open('start.log', 'r')for line in f.readlines():    do_sth(line)

但是对于好几G的巨型文本文件,这样就无法全部载入到内存, 可以使用下面的方法

with open("log.txt") as infile:    for line in infile:        do_something_with(line)

参考资料

http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python

http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python

python逐行读入大文件

相关文章:

你感兴趣的文章:

标签云: