Python io – BytesIO,StringIO

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 io

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

Let’s see the output for this program:

python IO BytesIO example

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 io

data = io.StringIO()
data.write('JournalDev: ')
print('Python.', file=data)

print(data.getvalue())

data.close()

Let’s see the output for this program:

python StringIO example

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 io

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

Let’s see the output for this program:

python io module read stringIO data

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

使用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 io

file = 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值