# 如何对迭代器做切片操作 f = open('test.log') # f.readlines() 一次性把文件读入到内存当中 # lines = list(f.readlines()) # print(lines[100:300]) # for line in f: # print(line) from itertools import islice # 100-300行 lines = islice(f, 100, 300) # 前300行 # lines = islice(f, 300) # 300行到尾部 # lines = islice(f, 300, None) # for line in lines: # print(line) l = range(20) t = iter(l) # print(list(t)) # islice 会消耗原来的迭代对象 for x in islice(t, 5, 10): print(x) for x in t: print(x)