将 io.StringIO 转换为 io.BytesIO [英] convert io.StringIO to io.BytesIO

查看:48
本文介绍了将 io.StringIO 转换为 io.BytesIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题:我得到了一个 StringIO 对象,我如何将其转换为 BytesIO?

original question: i got a StringIO object, how can i convert it into BytesIO?

更新:更普遍的问题是,如何转换二进制(编码)file-like 对象被解码为 python3 中的类文件 对象?

update: The more general question is, how to convert a binary (encoded) file-like object into decoded file-like object in python3?

我得到的天真的方法是:

the naive approach i got is:

import io
sio = io.StringIO('wello horld')
bio = io.BytesIO(sio.read().encode('utf8'))
print(bio.read())  # prints b'wello horld'

有没有更高效、更优雅的方式来做到这一点?上面的代码只是将所有内容读入内存,对其进行编码,而不是分块流式传输数据.

is there more efficient and elegant way of doing this? the above code just reads everything into memory, encodes it instead of streaming the data in chunks.

例如,对于反向问题 (BytesIO -> StringIO),存在一个类 - io.TextIOWrapper 就是这样做的(见这个答案)

for example, for the reverse question (BytesIO -> StringIO) there exist a class - io.TextIOWrapper which does exactly that (see this answer)

推荐答案

有趣的是,尽管这个问题看似合理,但要找出我需要转换 StringIOStringIO 的实际原因并不容易code> 转换为 BytesIO.两者基本上都是缓冲区,您通常只需要其中一个就可以对字节或文本进行一些额外的操作.

It's interesting that though the question might seem reasonable, it's not that easy to figure out a practical reason why I would need to convert a StringIO into a BytesIO. Both are basically buffers and you usually need only one of them to make some additional manipulations either with the bytes or with the text.

我可能错了,但我认为您的问题实际上是如何使用 BytesIO 实例,当您要传递给它的某些代码需要一个文本文件时.

I may be wrong, but I think your question is actually how to use a BytesIO instance when some code to which you want to pass it expects a text file.

在这种情况下,这是一个常见问题,解决方案是 codecs模块.

In which case, it is a common question and the solution is codecs module.

使用它的两种常见情况如下:

The two usual cases of using it are the following:

In [16]: import codecs, io

In [17]: bio = io.BytesIO(b'qwe\nasd\n')

In [18]: StreamReader = codecs.getreader('utf-8')  # here you pass the encoding

In [19]: wrapper_file = StreamReader(bio)

In [20]: print(repr(wrapper_file.readline()))
'qwe\n'

In [21]: print(repr(wrapper_file.read()))
'asd\n'

In [26]: bio.seek(0)
Out[26]: 0

In [27]: for line in wrapper_file:
    ...:     print(repr(line))
    ...:
'qwe\n'
'asd\n'

编写要写入的文件对象

In [28]: bio = io.BytesIO()

In [29]: StreamWriter = codecs.getwriter('utf-8')  # here you pass the encoding

In [30]: wrapper_file = StreamWriter(bio)

In [31]: print('жаба', 'цап', file=wrapper_file)

In [32]: bio.getvalue()
Out[32]: b'\xd0\xb6\xd0\xb0\xd0\xb1\xd0\xb0 \xd1\x86\xd0\xb0\xd0\xbf\n'

In [33]: repr(bio.getvalue().decode('utf-8'))
Out[33]: "'жаба цап\\n'"

这篇关于将 io.StringIO 转换为 io.BytesIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆