python 3.4以后StringIO和cStringIO就没有了,转移到 io,的StringIO和BytesIO

from io import StringIO

fp=StringIO( )

1.StringIO模块

StringIO用于像文件一样对字符串缓冲区或者叫做内存文件进行读写。

f = StringIO()      # ready for writing

f = StringIO(buf)   # ready for reading

f.close()           # explicitly release resources held

flag = f.isatty()   # always false

pos = f.tell()      # get current position

f.seek(pos)         # set current position

f.seek(pos, mode)   # mode 0: absolute; 1: relative; 2: relative to EOF

buf = f.read()      # read until EOF

buf = f.read(n)     # read up to n bytes

buf = f.readline()  # read until end of line (‘\n‘) or EOF

list = f.readlines()# list of f.readline() results until EOF

f.truncate([size])  # truncate file at to at most size (default: current pos)

f.write(buf)        # write at current position

f.writelines(list)  # for line in list: f.write(line)

f.getvalue()        # return whole file‘s contents as a string

在有些时候python调用shell命令或者使用socket向端口发送命令后返回一大串分行的字符串。就可以使用StringIO对这些字符串分行读写。

例如向ZooKeeper的端口发送mntr命令

import socket

s=socket.socket()

s.connect((‘localhost‘,2181))

s.send(‘mntr‘)

data_mntr=s.recv(2048)

s.close()

print data_mntr

# python test.py

zk_version3.4.6-1569965, built on 02/20/2014 09:09 GMT

zk_avg_latency0

zk_max_latency0

zk_min_latency0

zk_packets_received335

zk_packets_sent334

zk_num_alive_connections1

zk_outstanding_requests0

zk_server_statefollower

zk_znode_count17159

zk_watch_count0

zk_ephemerals_count1

zk_approximate_data_size6666471

zk_open_file_descriptor_count27

zk_max_file_descriptor_count102400

data_mntr返回的数据就是一个string buffer,如果需要对每行数据进行读写,就需要像文件那样使用readline()或者readlines()

#!/usr/bin/python

import socket

from StringIO import StringIO

s=socket.socket()

s.connect((‘localhost‘,2181))

s.send(‘mntr‘)

data_mntr=s.recv(2048)

s.close()

h=StringIO(data_mntr)

print h.readline()

print h.readlines()

# python test.py

zk_version3.4.6-1569965, built on 02/20/2014 09:09 GMT

[‘zk_avg_latency\t0\n‘, ‘zk_max_latency\t0\n‘, ‘zk_min_latency\t0\n‘, ‘zk_packets_received\t347\n‘, ‘zk_packets_sent\t346\n‘, ‘zk_num_alive_connections\t1\n‘, ‘zk_outstanding_requests\t0\n‘, ‘zk_server_state\tfollower\n‘, ‘zk_znode_count\t17159\n‘, ‘zk_watch_count\t0\n‘, ‘zk_ephemerals_count\t1\n‘, ‘zk_approximate_data_size\t6666471\n‘, ‘zk_open_file_descriptor_count\t27\n‘, ‘zk_max_file_descriptor_count\t102400\n‘]

2.cStringIO模块

cStringIO模块和StringIO模块功能类似。不过这个模块是用C语言编写的。

将以上的

from StringIO import StringIO

直接换成

from cStringIO import StringIO

python 的StringIO的更多相关文章

  1. python的StringIO

    有时候需要将 information 保存在本地,可以这样写: file = open("filename","w") file.close() file.cl ...

  2. Python中StringIO和BytesIO

    介绍一下Python在内存中读写数据,用到的模块是StringIO和BytesIO StringIO >>> from io import StringIO >>> ...

  3. python的StringIO模块

    StringIO经常被用来作字符串的缓存,因为StringIO的一些接口和文件操作是一致的,也就是说同样的代码,可以同时当成文件操作或者StringIO操作. 一.StringIO中的常用方法 1.r ...

  4. 学习Python:StringIO与cStringIO

    StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO.一个简单的例子,让你对StringIO有一个感性的认识: f ...

  5. python学习——StringIO和BytesIO

    StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. StringIO顾名思义就是在内存中读写str. 要把str写入StringIO,我们需要先创建一个StringIO,然后,像文 ...

  6. python模块StringIO和BytesIO

    StringIO和BytesIO StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. StringIO顾名思义就是在内存中读写str. 要把str写入StringIO,我们需要先创 ...

  7. python StringIO类

    python的stringIO类用来处理字符串,由于其操作类似文件操作,可以视为内存中的文件. 1.创建stringIO 2.常用操作: write,writelines.getvalue.seek. ...

  8. Python入门篇-StringIO和BytesIO

    Python入门篇-StringIO和BytesIO 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.StringIO(用于文本处理) 1>.使用案例 #!/usr/bin ...

  9. python gzip 压缩文件

    压缩数据创建gzip文件 先看一个略麻烦的做法 ? 1 2 3 4 5 6 import StringIO,gzip content = 'Life is short.I use python' zb ...

随机推荐

  1. 安装PYthon+Kivy环境(记录)

    在线翻译 https://www.bing.com/translator/ Cython 0.27 发布了.准确说Cython是单独的一门语言,专门用来写在Python里面import用的扩展库.实际 ...

  2. Dubbo学习参考

    参考博客: 小宝鸽:https://blog.csdn.net/u013142781/article/details/50387583 https://blog.csdn.net/u013142781 ...

  3. Spring Cloud微服务体系搭建

    前期架构设计图: 参考博文: Eureka相关: Eureka注册与发现(高可用注册中心.注册服务.Feign服务调用):https://blog.csdn.net/qq_32529383/artic ...

  4. JVM类加载机制总结

    1.运行时加载优点 提高灵活性,可以在运行时动态加载,连接.例子:面向接口编程,动态绑定实现类(但C++也有动态绑定,说明动态绑定不一定通过运行时加载Class字节码实现,也可能是机器码支持的) 2. ...

  5. C#用Linq查询Combox的数据源

    https://www.cnblogs.com/sufei/archive/2010/01/12/1645763.html var result =  ((DataTable) (this.ComSh ...

  6. 【jdk源码分析】ArrayList的size()==0和isEmpty()

    先看结果 分析源码 [jdk源码解析]jdk8的ArrayList初始化长度为0 java的基本数据类型默认值 无参构造 size()方法 isEmpty()方法

  7. HDU 6162 Ch's gift(树链剖分+线段树)

    题意: 已知树上的每个节点的值和节点之间的关系建成了一棵树,现在查询节点u到节点v的最短路径上的节点值在l到r之间的节点值的和. 思路: 用树链剖分将树映射到线段树上,线段树上维护3个值,max,mi ...

  8. LA 4254 处理器(二分+贪心)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. Jmeter 中对响应报文处理后断言用到BeanShell Assertion

    Jmeter中常用的断言可以是Response Assertion 如果需要对响应报文中的某个字符串进行解码,对解码之后的值在进行断言要怎么做呢? 仔细观察一下,可以用下面俩个元件 Regular E ...

  10. password_hash加密

    每次执行 password_hash('123456', PASSWORD_BCRYPT) 语句后,得到哈希值都不一样! 给密码做哈希之前,会先加入一个随机子串,因为加入的随机子串每次是不一样的,所以 ...