Python3 io文本及原始流I/O工具用法详解


Posted in Python onMarch 23, 2020

io模块在解释器的内置open()之上实现了一些类来完成基于文件的输入和输出操作。这些类得到了适当的分解,从而可以针对不同的用途重新组合——例如,支持向一个网络套接字写Unicode数据。

1.1 内存中的流

StringIO提供了一种很便利的方式,可以使用文件API(如read()、write()等)处理内存中的文本。有些情况下,与其他一些字符串连接技术相比,使用StringIO构造大字符串可以提供更好的性能。内存中的流缓冲区对测试也很有用,写入磁盘上真正的文件并不会减慢测试套件的速度。

下面是使用StringIO缓冲区的一些标准例子。

import io
# Writing to a buffer
output = io.StringIO()
output.write('This goes into the buffer. ')
print('And so does this.', file=output)
# Retrieve the value written
print(output.getvalue())
output.close() # discard buffer memory
 
# Initialize a read buffer
input = io.StringIO('Inital value for read buffer')
# Read from the buffer
print(input.read())

这个例子使用了read(),不过也可以用readline()和readlines()方法。StringIO类还提供了一个seek()方法,读取文本时可以在缓冲区中跳转,如果使用一种前向解析算法,则这个方法对于回转很有用。

Python3 io文本及原始流I/O工具用法详解

要处理原始字节而不是Unicode文本,可以使用BytesIO。

import io
# Writing to a buffer
output = io.BytesIO()
output.write('This goes into the buffer. '.encode('utf-8'))
output.write('ÁÇÊ'.encode('utf-8'))
# Retrieve the value written
print(output.getvalue())
output.close() # discard buffer memory
 
# Initialize a read buffer
input = io.BytesIO(b'Inital value for read buffer')
# Read from the buffer
print(input.read())

写入BytesIO实例的值一定是bytes而不是str。

Python3 io文本及原始流I/O工具用法详解

1.2 为文本数据包装字节流

原始字节流(如套接字)可以被包装为一个层来处理串编码和解码,从而可以更容易地用于处理文本数据。TextIOWrapper类支持读写。write_through参数会禁用缓冲,并且立即将写至包装器的所有数据刷新输出到底层缓冲区。

import io
# Writing to a buffer
output = io.BytesIO()
wrapper = io.TextIOWrapper(
  output,
  encoding='utf-8',
  write_through=True,
)
wrapper.write('This goes into the buffer. ')
wrapper.write('ÁÇÊ')
# Retrieve the value written
print(output.getvalue())
output.close() # discard buffer memory
 
# Initialize a read buffer
input = io.BytesIO(
  b'Inital value for read buffer with unicode characters ' +
  'ÁÇÊ'.encode('utf-8')
)
wrapper = io.TextIOWrapper(input, encoding='utf-8')
# Read from the buffer
print(wrapper.read())

这个例子使用了一个BytesIO实例作为流。对应bz2、http,server和subprocess的例子展示了如何对其他类型的类似文件的对象使用TextIOWrapper。

Python3 io文本及原始流I/O工具用法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
巧用Python装饰器 免去调用父类构造函数的麻烦
May 18 Python
Python将多个excel文件合并为一个文件
Jan 03 Python
tensorflow更改变量的值实例
Jul 30 Python
对pycharm 修改程序运行所需内存详解
Dec 03 Python
在Python中COM口的调用方法
Jul 03 Python
python支付宝支付示例详解
Aug 22 Python
在PyTorch中使用标签平滑正则化的问题
Apr 03 Python
Python实现文件压缩和解压的示例代码
Aug 12 Python
Python获取指定网段正在使用的IP
Dec 14 Python
pytorch 如何把图像数据集进行划分成train,test和val
May 31 Python
Django drf请求模块源码解析
Jun 08 Python
Python 避免字典和元组的多重嵌套问题
Jul 15 Python
python实现横向拼接图片
Mar 23 #Python
Python操作Excel工作簿的示例代码(\*.xlsx)
Mar 23 #Python
python实现拼接图片
Mar 23 #Python
python使用PIL剪切和拼接图片
Mar 23 #Python
python异常处理、自定义异常、断言原理与用法分析
Mar 23 #Python
python实现单张图像拼接与批量图片拼接
Mar 23 #Python
OpenCV Python实现拼图小游戏
Mar 23 #Python
You might like
用PHP ob_start()控制浏览器cache、生成html实现代码
2010/02/16 PHP
将一维或多维的数组连接成一个字符串的php代码
2010/08/08 PHP
php Xdebug的安装与使用详解
2013/06/20 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
PHP面向对象程序设计类的定义与用法简单示例
2016/12/27 PHP
My Desktop :) 桌面式代码
2008/12/29 Javascript
侧栏跟随滚动的简单实现代码
2013/03/18 Javascript
jQuery动态生成Bootstrap表格
2016/11/01 Javascript
javascript跨域请求包装函数与用法示例
2016/11/03 Javascript
JS IOS/iPhone的Safari浏览器不兼容Javascript中的Date()问题如何解决
2016/11/11 Javascript
js实现日历与定时器
2017/02/22 Javascript
详解vue.js2.0父组件点击触发子组件方法
2017/05/10 Javascript
Vue2仿淘宝实现省市区三级联动
2020/04/15 Javascript
js jquery 获取某一元素到浏览器顶端的距离实现方法
2018/09/05 jQuery
Node.js之readline模块的使用详解
2019/03/25 Javascript
详解微信小程序的不同函数调用的几种方法
2019/05/08 Javascript
JavaScript实现简单的图片切换功能(实例代码)
2020/04/10 Javascript
jquery实现简单拖拽效果
2020/07/20 jQuery
python条件和循环的使用方法
2013/11/01 Python
详细讲解Python中的文件I/O操作
2015/05/24 Python
python脚本设置系统时间的两种方法
2016/02/21 Python
Django框架实现的分页demo示例
2019/05/25 Python
python实现静态服务器
2019/09/05 Python
python实现按首字母分类查找功能
2019/10/31 Python
Python TCP通信客户端服务端代码实例
2019/11/21 Python
Jupyter Notebook输出矢量图实例
2020/04/14 Python
什么是GWT的Module
2013/01/20 面试题
法律专业应届本科毕业生求职信
2013/10/25 职场文书
《愚公移山》教学反思
2014/02/20 职场文书
建议书标准格式
2014/03/12 职场文书
竞选副班长演讲稿
2014/04/24 职场文书
服务员岗位职责范本
2015/04/09 职场文书
主持人大赛开场白
2015/05/29 职场文书
返乡农民工证明
2015/06/24 职场文书
导游词之湖州-太湖
2019/10/11 职场文书
详解Nginx的超时keeplive_timeout配置步骤
2022/05/25 Servers