匿名用户
1级
2017-08-26 回答
python中读取数据的时候有几种方法,无非是read,readline,readlings和xreadlines几种方法,在几种方法中,read和xreadlines可以作为迭代器使用,从而在读取大数据的时候比较有效果.
在测试中,先创建一个大文件,大概1GB左右,使用的程序如下:
[python] view plaincopyprint?
import os.path
import time
while os.path.getsize('messages') <1000000000:
f = open('messages','a')
f.write('this is a file/n')
f.close()
print 'file create complted'
在这里使用循环判断文件的大小,如果大小在1GB左右,那么结束创建文件。--需要花费好几分钟的时间。
测试代码如下:
[python] view plaincopyprint?
#22s
start_time = time.time()
f = open('messages','r')
for i in f:
end_time = time.time()
print end_time - start_time
break
f.close()