StringIO 模块用于在内存缓冲区中读写数据

模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中。
此类中的大部分函数都与对文件的操作方法类似。

例:
1      #coding=gbk
2       
3      import StringIO, cStringIO, sys
4       
5      s = StringIO.StringIO("JGood is a handsome boy")
6      s.write("JGood is a handsome boy \r\n")
7      s.write('okkkk中国')
8      s.seek(0)
9      print s.read()
10       
11      #最后4个字节
12      s.seek(-4, 2)
13      print s.read()
14       
15      #---- 结果 ----
16      #JGood is a handsome boy 
17      #okkkk中国
18      #中国

 

通过例子,我们看到了StringIO的行为,基本与file一致。StringIO提供了一个方法,可以方便的获取其中的数据:StringIO. getvalue()。如果使用read方法获取其中的数据,必须通过seek先设置"文件指针"的位置。

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


----------------------
s=StringIO.StrngIO([buf])
此实例类似于open方法,不同的是它并不会在硬盘中生成文件,而只寄存在缓冲区;可选参数buf是一个str或unicode类型。它将会与其他后续写入的数据存放在一起。
----------------------
StringIO类中的方法:
....● read
....● readline
....● readlines
....● write
....● writelines
....● getvalue
....● truncate
....● tell
....● seek
....● close
....● isatty
....● flush
----------------------
s.read([n])
参数n限定读取长度,int类型;缺省状态为从当前读写位置读取对象s中存储的所有数据。读取结束后,读写位置被移动。
----------------------
s.readline([length])
参数length限定读取的结束位置,int类型,缺省状态为None:从当前读写位置读取至下一个以“\n”为结束符的当前行。读写位置被移动。
----------------------
s.readlines([sizehint])
参数sizehint为int类型,缺省状态为读取所有行并作为列表返回,除此之外从当前读写位置读取至下一个以“\n”为结束符的当前行。读写位置被移动。
----------------------
s.write(s)
从读写位置将参数s写入给对象s。参数s为str或unicode类型。读写位置被移动。
----------------------
s.writelines(list)
从读写位置将list写入给对象s。参数list为一个列表,列表的成员为str或unicode类型。读写位置被移动。
----------------------
s.getvalue()
此函数没有参数,返回对象s中的所有数据。
----------------------
s.truncate([size])
从读写位置起切断数据,参数size限定裁剪长度,缺省值为None。
----------------------
s.tell()
返回当前读写位置。
----------------------
s.seek(pos[,mode])
移动当前读写位置至pos处,可选参数mode为0时将读写位置移动至pos处,为1时将读写位置从当前位置起向后移动pos个长度,为2时将读写位置置于末尾处再向后移动pos个长度;默认为0。
----------------------
s.close()
释放缓冲区,执行此函数后,数据将被释放,也不可再进行操作。
---------------------
s.isatty()
此函数总是返回0。不论StringIO对象是否已被close()。
----------------------
s.flush()
刷新内部缓冲区。
----------------------
dir(StringIO.StringIO)的返回值中还包含有一个test函数,不过不用理睬它,它没有任何意义

=====================================================

StringIO经常被用来作为字符串的缓存,应为StringIO有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作或者StringIO操作。比如:

import string, os, sys
import StringIO

def writedata(fd, msg):
    fd.write(msg)
    
= open('aaa.txt''w')

writedata(f, 
"xxxxxxxxxxxx")
f.close()

= StringIO.StringIO()
writedata(s, 
"xxxxxxxxxxxxxx")

    因为文件对象和StringIO大部分的方法都是一样的,比如read, readline, readlines, write, writelines都是有的,这样,StringIO就可以非常方便的作为"内存文件对象"。

  import string
import StringIO

= StringIO.StringIO()
s.write(
"aaaa")
lines 
= ['xxxxx''bbbbbbb']
s.writelines(lines)

s.seek(0)
print s.read()

print s.getvalue()
s.write(
" ttttttttt ")
s.seek(0)
print s.readlines()
print s.len


    StringIO还有一个对应的c语言版的实现,它有更好的性能,但是稍有一点点的区别,cStringIO没有len和pos属性。(还有,cStringIO不支持Unicode编码)

posted @ 2012-06-21 11:24  dkcndk  阅读(3210)  评论(0编辑  收藏  举报