Python StringIO模块实现在内存缓冲区中读写数据
作者:junjie 时间:2021-12-22 08:23:13
模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中。
此类中的大部分函数都与对文件的操作方法类似。
例:
#coding=gbk
import StringIO, cStringIO, sys
s = StringIO.StringIO("JGood is a handsome boy")
s.write("JGood is a handsome boy \r\n")
s.write('okkkk中国')
s.seek(0)
print s.read()
#最后4个字节
s.seek(-4, 2)
print s.read()
#---- 结果 ----
#JGood is a handsome boy
#okkkk中国
#中国
通过例子,我们看到了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)
f = open('aaa.txt', 'w')
writedata(f, "xxxxxxxxxxxx")
f.close()
s = StringIO.StringIO()
writedata(s, "xxxxxxxxxxxxxx")
因为文件对象和StringIO大部分的方法都是一样的,比如read, readline, readlines, write, writelines都是有的,这样,StringIO就可以非常方便的作为"内存文件对象"。
import string
import StringIO
s = 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编码)
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python的Flask框架实现视频的流媒体传输
![](https://img.aspxhome.com/file/2023/2/69512_0s.jpg)
php中实现记住密码自动登录的代码
python 按不同维度求和,最值,均值的实例
![](https://img.aspxhome.com/file/2023/9/59479_0s.jpg)
golang中的空slice案例
![](https://img.aspxhome.com/file/2023/3/101043_0s.png)
Python装饰器用法与知识点小结
asp如何实现按照输入汉字提示拼音功能?
Python使用plotly绘制数据图表的方法
![](https://img.aspxhome.com/file/2023/1/82721_0s.png)
PHP实现git部署的方法教程
详细总结Python类的多继承知识
![](https://img.aspxhome.com/file/2023/2/95412_0s.png)
Oracle不同数据库间对比分析脚本
python 对象真假值的实例(哪些视为False)
基于DataFrame改变列类型的方法
Python 数字转化成列表详情
![](https://img.aspxhome.com/file/2023/3/67373_0s.png)
Asp定时执行操作 Asp定时读取数据库(网页定时操作详解)
python中startswith()和endswith()的用法详解
Python的几种主动结束程序方式
![](https://img.aspxhome.com/file/2023/6/97766_0s.jpg)
pytorch 模型可视化的例子
![](https://img.aspxhome.com/file/2023/0/59480_0s.jpg)
ajax Google PageRank3(PR值)查询源代码
![](https://img.aspxhome.com/file/UploadPic/200711/4/2007114142753675s.gif)
numpy中的transpose函数中具体使用方法
![](https://img.aspxhome.com/file/2023/2/81582_0s.png)
python中实现迭代器(iterator)的方法示例
![](https://img.aspxhome.com/file/2023/9/64639_0s.png)