3-5 如何对迭代器进行切片操作

本文介绍了Python中多种文件读取方法,包括一次性读取所有内容、逐行读取及使用迭代器读取指定行数的方法,并对比了不同方法的优缺点。

>>> 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.read)
>>> 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.
help(f.readlines)

读取文本的最好方式还是使用迭代协议。

>>> 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
help (islice)

 

转载于:https://www.cnblogs.com/smulngy/p/8853641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值