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中死锁的形成示例及死锁情况的防止
Jun 14 Python
Python实现调度算法代码详解
Dec 01 Python
python:pandas合并csv文件的方法(图书数据集成)
Apr 12 Python
Python爬虫包BeautifulSoup学习实例(五)
Jun 17 Python
python使用pygame模块实现坦克大战游戏
Mar 25 Python
Python实现Singleton模式的方式详解
Aug 08 Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
Oct 16 Python
如何在python中实现随机选择
Nov 02 Python
如何运行带参数的python脚本
Nov 15 Python
python操作gitlab API过程解析
Dec 27 Python
Python 字典中的所有方法及用法
Jun 10 Python
python使用XPath解析数据爬取起点小说网数据
Apr 22 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.NET的入门教程
2006/10/09 PHP
php数字转汉字代码(算法)
2011/10/08 PHP
php_screw 1.5:php加密: 安装与使用详解
2013/06/20 PHP
PHP实现查询两个数组中不同元素的方法
2016/02/23 PHP
PHP 中 DOMDocument保存xml时中文出现乱码问题的解决方案
2016/09/19 PHP
php输出图像的方法实例分析
2017/02/16 PHP
PHP简单实现遍历目录下特定文件的方法小结
2017/05/22 PHP
分享5个非常有用的Laravel Blade指令
2018/05/30 PHP
Javascript Math ceil()、floor()、round()三个函数的区别
2010/03/09 Javascript
基于MooTools的很有创意的滚动条时钟动画
2010/11/14 Javascript
jquery Ajax 实现加载数据前动画效果的示例代码
2014/02/07 Javascript
JS获取地址栏参数的几种方法小结
2014/02/28 Javascript
jquery自动将form表单封装成json的具体实现
2014/03/17 Javascript
node.js中的http.request.end方法使用说明
2014/12/10 Javascript
自定义函数实现IE7与IE8不兼容js中trim函数的问题
2015/02/03 Javascript
jQuery ready()和onload的加载耗时分析
2016/09/08 Javascript
JS中位置与大小的获取方法
2016/11/22 Javascript
Vue-cli proxyTable 解决开发环境的跨域问题详解
2017/05/18 Javascript
深入理解ES7的async/await的用法
2017/09/09 Javascript
JavaScript检查数据中是否存在相同的元素(两种方法)
2018/10/07 Javascript
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
Python二叉树定义与遍历方法实例分析
2018/05/25 Python
用Python将mysql数据导出成json的方法
2018/08/21 Python
python+selenium实现自动抢票功能实例代码
2018/11/23 Python
python文档字符串(函数使用说明)使用详解
2019/07/30 Python
python 比较字典value的最大值的几种方法
2020/04/17 Python
解决python 执行shell命令无法获取返回值的问题
2020/12/05 Python
外贸业务员岗位职责
2013/11/24 职场文书
采购部岗位职责
2013/11/24 职场文书
情人节活动策划方案
2014/02/27 职场文书
2014年团总支工作总结
2014/11/21 职场文书
2014年招生工作总结
2014/11/26 职场文书
中班上学期个人总结
2015/02/12 职场文书
护士求职自荐信
2015/03/25 职场文书
MySQL系列之九 mysql查询缓存及索引
2021/07/02 MySQL
Win11 Build 22000.51版本文件资源管理器“命令栏”和上下文菜单有什么新变化?
2021/11/21 数码科技