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 pickle模块用法实例
Apr 14 Python
Python 实现一个颜色色值转换的小工具
Dec 06 Python
Python迭代器和生成器定义与用法示例
Feb 10 Python
Python生成器定义与简单用法实例分析
Apr 30 Python
python print 按逗号或空格分隔的方法
May 02 Python
python 实现登录网页的操作方法
May 11 Python
django 在原有表格添加或删除字段的实例
May 27 Python
Django如何开发简单的查询接口详解
May 17 Python
浅谈Python中(&,|)和(and,or)之间的区别
Aug 07 Python
Django ORM filter() 的运用详解
May 14 Python
pytorch DataLoader的num_workers参数与设置大小详解
May 28 Python
python中的3种定义类方法
Nov 27 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网站提速三大“软”招
2006/10/09 PHP
一些关于PHP的知识
2006/11/17 PHP
PHP在线生成二维码代码(google api)
2013/06/03 PHP
php继承中方法重载(覆盖)的应用场合
2015/02/09 PHP
thinkphp验证码的实现(form、ajax实现验证)
2016/07/28 PHP
php-beanstalkd消息队列类实例分享
2017/07/19 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
jQuery 连续列表实现代码
2009/12/21 Javascript
深入理解JSON数据源格式
2014/01/10 Javascript
Jquery插件实现点击获取验证码后60秒内禁止重新获取
2015/03/13 Javascript
AngularJs Modules详解及示例代码
2016/09/01 Javascript
Bootstrap模态框禁用空白处点击关闭
2016/10/20 Javascript
js实现控制文件拖拽并获取拖拽内容功能
2018/02/17 Javascript
VUE简单的定时器实时刷新的实现方法
2019/01/20 Javascript
[02:32]DOTA2完美大师赛场馆静安体育中心观赛全攻略
2017/11/08 DOTA
python中子类调用父类函数的方法示例
2017/08/18 Python
python如何实现反向迭代
2018/03/20 Python
实践Vim配置python开发环境
2018/07/02 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
2020/04/14 Python
用python写爬虫简单吗
2020/07/28 Python
python中uuid模块实例浅析
2020/12/29 Python
罗兰·穆雷官网:Roland Mouret
2018/09/28 全球购物
XD健身器材:Kevlar球、Crossfit健身球
2019/03/26 全球购物
应届生会计求职信
2013/11/11 职场文书
《夕阳真美》教学反思
2014/04/27 职场文书
在校实习生求职信
2014/06/18 职场文书
党员群众路线对照检查材料
2014/08/31 职场文书
学习张丽丽心得体会
2014/09/03 职场文书
机关中层领导干部群众路线教育实践活动个人对照检查材料
2014/09/24 职场文书
药店采购员岗位职责
2014/09/30 职场文书
六五普法宣传标语
2014/10/06 职场文书
降价通知函
2015/04/23 职场文书
公司规章制度范本
2015/08/03 职场文书
2016教师节感恩话语
2015/12/09 职场文书
2016年优秀共产党员先进事迹材料
2016/02/29 职场文书
td 内容自动换行 table表格td设置宽度后文字太多自动换行
2022/12/24 HTML / CSS