Python io module allows us to manage the file-related input and output operations. The advantage of using the IO module is that the classes and functions available allows us to extend the functionality to enable writing to the Unicode data.

Python io模块允许我们管理与文件相关的输入和输出操作。 使用IO模块的优势在于,可用的类和函数使我们能够扩展功能,从而能够写入Unicode数据。

Python IO模块 (Python IO Module)

There are many ways in which we can use the io module to perform stream and buffer operations in Python. We will demonstrate a lot of examples here to prove the point. Let’s get started.

我们可以使用多种方式使用io模块在Python中执行流和缓冲区操作。 我们将在此处演示许多示例以证明这一点。 让我们开始吧。

Python字节IO (Python BytesIO)

Just like what we do with variables, data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations.

就像我们对变量所做的一样,当我们使用io模块的字节IO操作时,数据可以作为字节保存在内存中的缓冲区中。

Here is a sample program to demonstrate this:

这是一个示例程序来演示这一点:

import iostream_str = io.BytesIO(b"JournalDev Python: \x00\x01")
print(stream_str.getvalue())

Let’s see the output for this program:

The getvalue() function just takes the value from the Buffer as a String.

让我们看一下该程序的输出:

getvalue()函数只是将Buffer中的值作为字符串。

Python StringIO (Python StringIO)

We can even use StringIO as well which is extremely similar in use to BytesIO. Here is a sample program:

我们甚至可以使用StringIO ,它在使用上与BytesIO非常相似。 这是一个示例程序:

import iodata = io.StringIO()
data.write('JournalDev: ')
print('Python.', file=data)print(data.getvalue())data.close()

Let’s see the output for this program:

Notice that we even closed the buffer after we’re done with the buffer. This helps save buffer memory as they store data in-memory.

让我们看一下该程序的输出:

请注意,在使用完缓冲区后,我们甚至关闭了缓冲区。 当缓冲区将数据存储在内存中时,这有助于节省缓冲区内存。

Also, we used the print method with an optional argument to specify an IO stream of the variable, which is perfectly compatible with a print statement.

另外,我们使用带有可选参数的print方法来指定变量的IO流,该流与print语句完全兼容。

使用StringIO阅读 (Reading using StringIO)

Once we write some data to the StringIO buffer, we can read it as well. Let’s look at a code snippet:

一旦将一些数据写入StringIO缓冲区,我们也可以读取它。 让我们看一下代码片段:

import ioinput = io.StringIO('This goes into the read buffer.')
print(input.read())

Let’s see the output for this program:

让我们看一下该程序的输出:

使用StringIO读取文件 (Reading file using StringIO)

It is also possible to read a file and stream it over a network as Bytes. The io module can be used to convert a media file like an image to be converted to bytes. Here is a sample program:

也可以读取文件并通过网络以字节为单位进行流传输。 io模块可用于将媒体文件(如图像)转换为字节。 这是一个示例程序:

import iofile = io.open("whale.png", "rb", buffering = 0)
print(file.read())

Let’s see the output for this program:

For this program to run, we had a whale.png image present in our current directory.

让我们看一下该程序的输出:

为了运行该程序,当前目录中存在一个whale.png图像。

io.open()与os.open() (io.open() vs os.open())

The io.open() function is a much preferred way to perform I/O operations as it is made as a high-level interface to peform file I/O. It wraps the OS-level file descriptor in an object which we can use to access the file in a Pythonic way.

io.open()函数是执行I / O操作的首选方法,因为它是peform文件I / O的高级接口。 它将OS级文件描述符包装在一个对象中,我们可以使用它以Python方式访问文件。

The os.open() function takes care of the lower-level POSIX syscall. It takes input POSIX based arguments and returns a file descriptor which represents the opened file. It does not return a file object; the returned value will not have read() or write() functions.

os.open()函数负责较低级别的POSIX系统调用。 它接受基于POSIX的输入参数,并返回代表打开文件的文件描述符。 它不返回文件对象。 返回的值将没有read()write()函数。

Overall, io.open() function is just a wrapper over os.open() function. The os.open() function just also sets default config like flags and mode too while io.open() doesn’t to it and depends on the values passed to it.

总体而言, io.open()函数只是os.open()函数的包装。 os.open()函数也只设置默认配置,例如标志和模式,而io.open()则不设置默认配置,它取决于传递给它的值。

结论 (Conclusion)

In this lesson, we studied simple operations of python IO module and how we can manage the Unicode characters with BytesIO as well. However, if you are looking for complete file operations such as delete and copy a file then read python read file.

在本课程中,我们研究了python IO模块的简单操作,以及如何使用BytesIO管理Unicode字符。 但是,如果您要查找完整的文件操作(例如删除和复制文件),请阅读python read file 。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/19178/python-io-bytesio-stringio

Python io – BytesIO,StringIO相关推荐

  1. python使用BytesIO或StringIO读写文件

    ByteIO与StringIO处理数据是在内存中完成的,需要用到的包是io 二进制文件ByteIO import iofile = open("test_pdf.pdf", 'br ...

  2. python中IO库中StringIO方法和BytesIO方法用法详解

    有时候数据读写不一定是文件,也可以在内存中读写.StringIO顾名思义就是在内存中读写str. Python在内存中读写数据,用到的模块是StringIO和BytesIO. getvalue()方法 ...

  3. python io模块_python中的StringIO模块

    原博文 2015-10-23 15:21 − # python中的StringIO模块 标签:python StringIO --- > 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的, ...

  4. Python文件读写、StringIO和BytesIO

    1 IO的含义 在计算机中,IO是Input/Output的简写,也就是输入和输出. 由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就 ...

  5. python基础-BytesIO,StringIO

    StringIO 常用方法 示例代码 strIO截取 利用缓存写入文件 BytesIO 通过缓存存入文件 通过缓存读取文件 文件句柄赋值BytesIO 想要了解文件读取操作的,可以参考 文件读取 文件 ...

  6. Python3调试类_io.BytesIO、_io.StringIO用法(Python3 IO编程之StringIO和BytesIO)

    StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. 要把str写入StringIO,我们需要先创建一个StringIO,然后像文件一样写入即可 >>> from ...

  7. Python3 IO编程之StringIO和BytesIO

    StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. 要把str写入StringIO,我们需要先创建一个StringIO,然后像文件一样写入即可 >>> from ...

  8. python asyncio与aiohttp_python链家网异步IO爬虫,使用asyncio、aiohttp和aiomysql

    python链家网异步IO爬虫,使用asyncio.aiohttp和aiomysql 平均1秒可以爬取30个详情页信息 可以使用asyncio.Semaphore来控制并发数,达到限速的效果 # -* ...

  9. python 使用cv2、io.BytesIO处理图片二进制数据

    参考资料:http://www.voidcn.com/article/p-sabqihis-bry.html 参考资料: https://blog.csdn.net/yang_bingo/articl ...

最新文章

  1. Webpack中Loader和Plugin的区别和编写思路
  2. 【文本分类】基于BERT预训练模型的灾害推文分类方法、基于BERT和RNN的新闻文本分类对比
  3. 您准备好观看GraphQL了吗?
  4. java电影票选座_jQuery实现电影票选座效果手机端代码
  5. long类型怎么转换成string_Python知识点-Python变量类型有哪些?
  6. checkbox全选和反选功能
  7. CTS(2)---CTS、CTS-V、GTS版本更新,及其注意事项
  8. JS之Window对象
  9. Java学习之==注释、数据类型、变量、运算符
  10. 证明:一个有n个结点的非空二叉树的高度至少为lgn
  11. GitHub上Java捕鱼达人源码分析(fishlord)
  12. Mysql 子查询 (查询语句中带查询语句)
  13. Visual Studio C++ 输出调试信息在调试-输出窗口
  14. NX二次开发CreateDialog函数在UI.hxx文件和WinUser.h中的冲突
  15. JS内置对象及其用法总结
  16. 企立方:拼多多点击率问题
  17. 冻结步态你了解多少呢?
  18. 深度学习(花书)笔记2——(无)监督学习算法介绍
  19. 特别关注 | 想给基因取个名儿?也要按照基本法!
  20. 使用paddle框架无人驾驶 —— 卷积神经网络计算方向盘打角

热门文章

  1. html5 video标签兼容性与自定义控件
  2. 一种简单的排列组合方法实现(C语言)
  3. MyEclipse常用设置方法
  4. C语言的关键字 详解
  5. [转载] OpenCV-Python 图像处理(二):图像的读取、显示与保存
  6. [转载] python numpy 总结
  7. 博客园五月纪念日——去你的写博无用论
  8. Spring的数据库编程浅入浅出——不吹牛逼不装逼
  9. Python安装numpy模块及pycharm配置
  10. 第三节基础篇—SQL的约束