python模块之StringIO
来源:互联网 发布:淘宝网自动发货软件 编辑:程序博客网 时间:2024/05/16 23:54
StringIO经常被用来作为字符串的缓存,应为StringIO有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作或者StringIO操作。比如:
因为文件对象和StringIO大部分的方法都是一样的,比如read, readline, readlines, write, writelines都是有的,这样,StringIO就可以非常方便的作为"内存文件对象"。
StringIO还有一个对应的c语言版的实现,它有更好的性能,但是稍有一点点的区别,cStringIO没有len和pos属性。
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")
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
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属性。
- python模块之StringIO
- python模块之StringIO
- python模块之StringIO
- Python模块之StringIO【转】
- Python 模块学习 ---- StringIO
- python中的StringIO模块
- python之StringIO
- Python(StringIO)模块详解
- Python(StringIO)模块详解
- Python模块学习 -- StringIO, cStringIO 内存文件
- Python模块学习 ---- StringIO, cStringIO 内存文件
- Python模块学习 ---- StringIO, cStringIO 内存文件
- python中的StringIO和cStringIO模块
- [笔记]python的StringIO与BytesIO模块
- Python小模块--StringIO
- python StringIO
- python StringIO
- Python 标准库 -> StringIO模块 And cStringIO模块
- Ajax技术
- 没有文件扩展“.js”的脚本引擎--解决办法(downmoon)
- 用冰刃结束pubwin
- 程序员思维定势
- 三种禁用FileSystemObject组件的方法
- python模块之StringIO
- 使用触发器实现数据库级守护,防止DDL操作
- -generating artifacts的一种解决办法
- 用filter解决汉字编码及禁止页面缓存 和 用listener实现在线人数统计
- 在ASP.NET中创建安全的web站点
- 关于"深入浅出MFC"的几个问题
- ASP.NET处理浏览器中数据分页的方法
- Java ToyProgam LunacyDoctor implements IO and Sequence Stack
- 中外主要节日表