• Python StringIO实现内存缓冲区中读写数据


    StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以像操作磁盘文件那样来操作StringIO。这篇文章主要介绍了Python StringIO模块,此模块主要用于在内存缓冲区中读写数据。模块中只有一个StringIO类,所以它的可用方法都在类中,此类中的大部分函数都与对文件的操作方法类似。

    ----------------------------------

    s=StringIO.StringIO([buf])
    此实例类似于open方法,不同的是它并不会在硬盘中生成文件,而只寄存在缓冲区;可选参数buf是一个str或unicode类型。它将会与其他后续写入的数据存放在一起(注意,若要在初始化数据之后继续写入数据,则在写入数据之前,应先将读写位置移动到结尾,然后再写入,否则,初始化数据会被覆盖掉,因为读写位置默认是0)。

    StringIO类中的方法:
    s.read([n])
    参数n限定读取长度,int类型;缺省状态为从当前读写位置读取对象s中存储的所有数据。读取结束后,读写位置被移动。
    ----------------------
    s.readline([length])
    参数length限定读取的结束位置,int类型,缺省状态为None:从当前读写位置读取至下一个以“ ”为结束符的当前行。读写位置被移动。
    ----------------------
    s.readlines([sizehint])
    参数sizehint为int类型,缺省状态为读取所有行并作为列表返回,除此之外从当前读写位置读取至下一个以“ ”为结束符的当前行。读写位置被移动。
    ----------------------
    s.write(s)
    从读写位置将参数s写入给对象s。参数s为str或unicode类型。读写位置被移动。
    ----------------------
    s.writelines(list)
    从读写位置将list写入给对象s。参数list为一个列表,列表的成员为str或unicode类型。读写位置被移动。
    ----------------------
    s.getvalue()
    此函数没有参数,无论读写位置在哪里,都能够返回对象s中的所有数据。
    ----------------------
    s.truncate([size])
    1》有size参数

    无论读写位置在哪里,都从起始位置开始,裁剪size字节的数据。

    2》不带size参数

    将当前读写位置之前的数据,裁剪下来。
    ----------------------
    s.tell()
    返回当前读写位置。
    ----------------------
    s.seek(pos[,mode])
    移动当前读写位置至pos处,可选参数mode为0时将读写位置移动至pos处,
    为1时将读写位置从当前位置起向前或向后移动|pos|个长度,
    为2时将读写位置置于末尾处再向前或向后移动|pos|个长度;

    mode的默认值为0。
    ----------------------
    s.close()
    释放缓冲区,执行此函数后,数据将被释放,也不可再进行操作。
    ----------------------
    s.isatty()
    此函数总是返回0。
    ----------------------
    s.flush()
    刷新内部缓冲区。
    ----------------------

    实例1:

    [python] view plain copy
     
    1. def writedata(file, msg):  
    2.     file.write(msg)  
    3.      
    4. f = open(r'C:Users91135Desktop est.txt', 'w')  
    5. writedata(f, "xxxxx!!!!")  
    6. f.close()  
    7. f = open(r'C:Users91135Desktop est.txt', 'r')  
    8. print f.read()  
    9. f.close()  

    实例2:

    [python] view plain copy
     
    1. import StringIO  
    2. def writedata(file, msg):  
    3.     file.write(msg)  
    4.      
    5. s = StringIO.StringIO('python')  
    6. print s.tell()#读写位置默认是0,因此,之后写入的数据("xxxxx!!!xxxxxx")会将之前的数据('python')覆盖掉  
    7. writedata(s, "xxxxx!!!xxxxxx")  
    8. print s.getvalue()  

    实例3:

    [python] view plain copy
     
    1. import StringIO  
    2. s = StringIO.StringIO('python')  
    3. s.seek(0,2)#将读写位置移动到结尾  
    4. s.write("aaaa")  
    5. lines = ['xxxxx', 'bbbbbbb']  
    6. s.writelines(lines)  
    7. s.write("ttttttttt")  
    8. print s.getvalue()  
    9. #如果使用read方法获取其中的数据,必须通过seek先设置"文件指针"的位置。  
    10. s.seek(0,0)#使用s.read()来读取所有数据前,应将读写位置移动到开头  
    11. print s.read()  
    12. print s.len  

    实例4:

    [python] view plain copy
     
    1. import StringIO  
    2. s = StringIO.StringIO("python")   
    3. #读写位置默认是0,下面的语句在写入数据时,并没有移动读写位置,因此,之前的数据("python")会被覆盖掉。  
    4. s.write("hello python!")    
    5. s.write('hello world!')    
    6. s.seek(0)    
    7. print s.read()    
    8. s.seek(-4,2)#移动读写位置,以便读取最后4个字节   
    9. print s.read()  

    通过例子,我们看到了StringIO的行为,基本与file一致。StringIO提供了一个方法,无论读写位置在哪里,都可以方便的获取其中的数据:StringIO.getvalue()。

    Python标准模块中还提供了一个cStringIO模块,它的行为与StringIO基本一致,但运行效率方面比StringIO更好。但使用cStringIO模块时,有几个注意点:
    1. cStringIO.StringIO不能作为基类被继承;
    2. 创建cStringIO.StringIO对象时,如果初始化函数提供了初始化数据,新生成的对象是只读的。所以下面的代码是错误的:

    [python] view plain copy
     
      1. import cStringIO  
      2. s = cStringIO.StringIO("python");   
      3. print s  
      4. print type(s)  
      5. print s.getvalue()  
      6. s.write("OOOKKK");#AttributeError: 'cStringIO.StringI' object has no attribute 'write'  

    练习使用记录:

    In [182]: import StringIO

    In [183]: s = StringIO.StringIO('this is a test')

    In [184]: print s.getvalue()
    this is a test

    In [185]: s.tell()
    Out[185]: 0

    In [186]: s.seek(15,1)

    In [187]: print s.tell()
    15

    In [188]: print s.getvalue()
    this is a test

    In [189]: s.write('this is also a test')

    In [190]: print s.read
    <bound method StringIO.read of <StringIO.StringIO instance at 0x03F32030>>

    In [191]: print s.read()


    In [192]: print s.getvalue()
    this is a test this is also a test

    In [193]: print s.tell()
    34

    In [194]: print s.seek(10,2)
    None

    In [195]: s.seek(-10,2)

    In [196]: print s.read()
    lso a test

    In [197]: print s.getvalue
    <bound method StringIO.getvalue of <StringIO.StringIO instance at 0x03F32030>>

    In [198]: print s.getvalue()
    this is a test this is also a test

    转发自http://blog.csdn.net/sxingming/article/details/52183563

  • 相关阅读:
    算法 python实现(二) 冒泡排序
    算法 python实现(一) 基本常识
    python 零散记录(五) import的几种方式 序列解包 条件和循环 强调getattr内建函数
    IT桔子-抓取数据
    mac中导出CSV格式在excel中乱码
    phantomJS安装
    pyquery 安装
    mac终端的命令都失效的解决方法
    Python爬虫防封杀方法集合
    python爬虫 403 Forbidden 解决方法
  • 原文地址:https://www.cnblogs.com/misswangxing/p/7921665.html
Copyright © 2020-2023  润新知