通用的方法:
f = open('/var/log/dpkg.log') lines = f.readlines() lines[100:300]
使用 readlines 会一致性地将文件导入内存当中,当文件非常大的时候,如某些日志文件,大小可能在几个G的,这样一致性地读入文件的操作方式是不可取的,可能导致内存不足
将文件的指针返回到文件的头部(初始位置)
f.seek(0)
from itertools import islice for line in islice(f,100,300): print(line) In [89]: islice? Type: type String form: Init definition: islice(self, *args, **kwargs) Docstring: islice(iterable, stop) --> islice object islice(iterable, start, stop[, step]) --> islice object Return an iterator whose next() method returns selected values from an iterable. If start is specified, will skip all preceding elements; otherwise, start defaults to zero. Step defaults to one. If specified as another value, step determines how many values are skipped between successive calls. Works like a slice() on a list but returns an iterator.
前500行
In [90]: islice(f,500) Out[90]: <itertools.islice at 0xb5c85edc>
100到末行
In [91]: islice(f,100,None) Out[91]: <itertools.islice at 0xb5c85bbc>
对于负引索是不支持的
In [92]: islice(f,100,-100) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in () ----> 1 islice(f,100,-100) ValueError: Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.
islice会消耗内部的生成器
In [99]: l = list(range(20)) In [100]: l Out[100]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] In [101]: t = iter(l) In [102]: t Out[102]: In [103]: for x in islice(t,5,10): .....: print(x) .....: 5 6 7 8 9 In [104]: for x in t: .....: print(x) .....: 10 11 12 13 14 15 16 17 18 19
迭代器对象(Python3.x)
In [109]: iter(l) is l Out[109]: False In [110]: t = iter(l) In [111]: t.__next__() Out[111]: 0 In [112]: t.__next__() Out[112]: 1 In [113]: t.__next__() Out[113]: 2 In [114]: next(t) Out[114]: 3 In [115]: next(t) Out[115]: 4 In [116]: next(t) Out[116]: 5
迭代器对象(Python2.x)
In [109]: iter(l) is l Out[109]: False In [110]: t = iter(l) In [111]: t.next() Out[111]: 0 In [112]: t.next() Out[112]: 1 In [113]: t.next() Out[113]: 2 In [114]: next(t) Out[114]: 3 In [115]: next(t) Out[115]: 4 In [116]: next(t) Out[116]: 5