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批量生成任意尺寸的图片
Aug 29 Python
浅谈Python类的__getitem__和__setitem__特殊方法
Dec 25 Python
基于python元祖与字典与集合的粗浅认识
Aug 23 Python
Python两个字典键同值相加的几种方法
Mar 05 Python
python list转置和前后反转的例子
Aug 26 Python
关于pycharm中pip版本10.0无法使用的解决办法
Oct 10 Python
Python如何基于rsa模块实现非对称加密与解密
Jan 03 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
Jan 09 Python
python GUI库图形界面开发之PyQt5访问系统剪切板QClipboard类详细使用方法与实例
Feb 27 Python
python pandas dataframe 去重函数的具体使用
Jul 20 Python
使用Python pip怎么升级pip
Aug 11 Python
Python爬虫爬取微博热搜保存为 Markdown 文件的源码
Feb 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处理单文件、多文件上传代码分享
2016/08/24 PHP
PHP数据对象PDO操作技巧小结
2016/09/27 PHP
laravel 判断查询数据库返回值的例子
2019/10/11 PHP
Code:findPosX 和 findPosY
2006/12/20 Javascript
用javascript实现自定义标签
2007/05/08 Javascript
jQuery下扩展插件和拓展函数的写法(匿名函数使用的典型例子)
2010/10/20 Javascript
js里的prototype使用示例
2010/11/19 Javascript
获取内联和链接中的样式(js代码)
2013/04/11 Javascript
JS可以控制样式的名称写法一览
2014/01/16 Javascript
js实现按Ctrl+Enter发送效果
2014/09/18 Javascript
js上传图片及预览功能实例分析
2015/04/24 Javascript
无循环 JavaScript(map、reduce、filter和find)
2017/04/08 Javascript
jquery仿京东商品放大浏览页面
2017/06/06 jQuery
浅谈实现vue2.0响应式的基本思路
2018/02/13 Javascript
原生JS实现动态加载js文件并在加载成功后执行回调函数的方法
2020/12/30 Javascript
浅谈React Native 传参的几种方式(小结)
2019/05/21 Javascript
优化Vue项目编译文件大小的方法步骤
2019/05/27 Javascript
js+canvas实现两张图片合并成一张图片的方法
2019/11/01 Javascript
Python中偏函数用法示例
2018/06/07 Python
Python Requests库基本用法示例
2018/08/20 Python
python 检查是否为中文字符串的方法
2018/12/28 Python
Django多数据库的实现过程详解
2019/08/01 Python
Python 操作 ElasticSearch的完整代码
2019/08/04 Python
python用opencv完成图像分割并进行目标物的提取
2020/05/25 Python
python3环境搭建过程(利用Anaconda+pycharm)完整版
2020/08/19 Python
IE浏览器单独写CSS样式的几种方法
2014/10/14 HTML / CSS
html5将图片转换成base64的实例代码
2016/09/21 HTML / CSS
欧洲领先的电子和电信零售商和服务提供商:Currys PC World Business
2017/12/05 全球购物
美国室内和室外装饰花盆购物网站:ePlanters
2019/03/22 全球购物
大学生优秀团员事迹材料
2014/01/30 职场文书
幼儿园教师奖惩制度
2014/02/01 职场文书
内蒙古鄂尔多斯市市长寄语
2014/04/10 职场文书
退休党员个人对照检查材料思想汇报
2014/09/29 职场文书
2014年圣诞节寄语
2014/12/08 职场文书
Java 超详细讲解设计模式之中的抽象工厂模式
2022/03/25 Java/Android
nginx静态资源的服务器配置方法
2022/07/07 Servers