Python(StringIO)模块详解
来源:互联网 发布:java可视化开发工具 编辑:程序博客网 时间:2024/06/06 09:32
很多时候,数据读写不一定是文件,也可以在内存中读写。
StringIO顾名思义就是在内存中读写str。
要把str写入StringIO,我们需要先创建一个StringIO,然后,像文件一样写入即可:
>>> from io import StringIO>>> f = StringIO()>>> f.write('hello')5>>> f.write(' ')1>>> f.write('world!')6>>> print(f.getvalue())hello world!
getvalue()方法用于获得写入后的str。
要读取StringIO,可以用一个str初始化StringIO,然后,像读文件一样读取:
>>> from io import StringIO>>> f = StringIO('Hello!\nHi!\nGoodbye!')>>> while True:... s = f.readline()... if s == '':... break... print(s.strip())...Hello!Hi!Goodbye!
参考资料:廖雪峰老师的官网
参考资料:详细教程
阅读全文
1 0
- 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模块(或Six.BytesIO()模块)实现在内存缓冲区中读写数据
- python StringIO
- python StringIO
- Python 标准库 -> StringIO模块 And cStringIO模块
- 初学者,SDN平台搭建
- jvm 五个组成部分
- SimpleAdapter
- NYOJ 最大岛屿
- 数据结构之排序5--快速排序
- Python(StringIO)模块详解
- 剑指offer35题(第一个只出现一次的字符)
- tomcat
- HDOJ 1017 A Mathematical Curiosity
- centos7集群中NTP设置时间同步
- Tomcat部署SSL使其支持https
- TF-IDF与余弦相似性的应用(一):自动提取关键词
- HEVC 帧内预测xPredIntraAng
- 深度学习总结(五)——各优化算法