您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python模块之StringIO使用示例

2024/12/1 9:03:58发布13次查看
stringio经常被用来作为字符串的缓存,应为stringio有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作或者stringio操作。比如:
复制代码 代码如下:
import string, os, sys
import stringiodef 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 stringios = 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属性。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product