>>> f = open('C:\Mynetstat.LOG') >>> lines = f.readlines() >>> lines >>> lines[10:20]
可以用切片的方式,但缺点是 f.readlines()是一次性读出文件所有内容,按行保存到列表 中。如果文件过大,会非常占用内存。f.read()返回的是字符串。


>>> help(f.read) Help on built-in function read: read(...) read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given.


>>> help(f.readlines) Help on built-in function readlines: readlines(...) readlines([size]) -> list of strings, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned.
读取文本的最好方式还是使用迭代协议。
>>> f.seek(0) #将文件指针指向头部 >>> for line in f : #对文件描述符进行迭代,打印输出 print line >>> from itertools import islice >>> f.seek(0) >>> for x in islice(f,10,20): #打印 10到20行 print x
输出结果:
TCP 0.0.0.0:49152 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49153 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49154 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49155 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49156 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5037 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5939 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5939 127.0.0.1:55947 ESTABLISHED
TCP 127.0.0.1:5939 127.0.0.1:59695 ESTABLISHED
TCP 127.0.0.1:7475 0.0.0.0:0 LISTENING
>>> f.seek(0) >>> for x in islice(f,20): #打印0到20行 print x
输出结果:
活动连接
协议 本地地址 外部地址 状态
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:902 0.0.0.0:0 LISTENING
TCP 0.0.0.0:912 0.0.0.0:0 LISTENING
TCP 0.0.0.0:7275 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49152 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49153 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49154 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49155 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49156 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5037 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5939 0.0.0.0:0 LISTENING
TCP 127.0.0.1:5939 127.0.0.1:55947 ESTABLISHED
TCP 127.0.0.1:5939 127.0.0.1:59695 ESTABLISHED
TCP 127.0.0.1:7475 0.0.0.0:0 LISTENING
>>> islice(f,20,None) #描述20行到最后 <itertools.islice object at 0x02BE2C60>
注:不支持负索引,读完文件之前不知道文件总共有多少行,所以不支持负索引
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] >>> t = iter(l) #生成一个迭代器对象 >>> for x in islice(t,5,10): #islice做为切片消耗原迭代对象。T仍然从0开始迭代,不过它把值扔掉,直到匹配到,start为止 print x,
输出结果:5 6 7 8 9
继续对t进行迭代,执行
>>> for x in t : print x,
输出结果:10 11 12 13 14 15 16 17 18 19
可以看出现在对t迭代是从序号0开始的,之前的迭代序号在累积。与文件指针有点相似。


>>> help (islice) Help on class islice in module itertools: class islice(__builtin__.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. | | Methods defined here: | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __iter__(...) | x.__iter__() <==> iter(x) | | next(...) | x.next() -> the next value, or raise StopIteration | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T