08 Feb 2017
打开一个文本文件,我们需要得到其中100-300行之间的内容,python中的文本文件是可迭代对象,我们是否可以使用类似列表的切片方式得到一个100-300行内容的生成器?
# 1. 使用islice获取文件的100-300行内容 >>> f = open('/var/log/dmesg') >>> from itertools import islice >>> islice.__doc__ 'islice(iterable, [start,] stop [, step]) --> islice object\n\nReturn an iterator whose next() method returns selected values from an\niterable. If start is specified, will skip all preceding elements;\notherwise, start defaults to zero. Step defaults to one. If\nspecified as another value, step determines how many values are \nskipped between successive calls. Works like a slice() on a list\nbut returns an iterator.' >>> for i in islice(f, 100, 300): ... print i ... Policy zone: DMA32 Kernel command line: ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet PID hash table entries: 2048 (order: 2, 16384 bytes) x86/fpu: xstate_offset[2]: 0240, xstate_sizes[2]: 0100 ... # 如果需要获取前500行, islice(f, 500) # 如果需要获取100到最后,islice(f, 100, None) # 2. 需要注意islice对原迭代器的消耗 >>> l = [x for x in xrange(20)] >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> il = iter(l) >>> for i in islice(il, 5, 10): ... print i ... 5 6 7 8 9 >>> for j in il: ... print j ... 10 11 12 13 14 15 16 17 18 19 # islice会对迭代器进行消耗,打开的文件也一样,所以每次需要对islice()中的迭代器进行重新申请 # 对于文件来说,可以使用f.seek(0)