1. 最基本的读文件方法:
<span style="font-size:14px;"># File: readline-example-1.py
file = open("sample.txt")
while 1:
line = file.readline()
if not line:
break
pass # do something</span>
一行一行得从文件读数据,显然比较慢;不过很省内存。在我的机器上读10M的sample.txt文件,每秒大约读32000行