Shm*_*ikA 15 python io encoding stream python-3.x
original question: i got a StringIO object, how can i convert it into BytesIO?
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'
Run Code Online (Sandbox Code Playgroud)
is there more elegant way of doing this?
for example, for the reverse question (BytesIO -> StringIO) there exist a class - io.TextIOWrapper which does exactly that (see this answer)
有趣的是,尽管这个问题看似合理,但要找出将a转换StringIO为a 的实际原因并不是一件容易的事BytesIO。两者基本上都是缓冲区,通常只需要其中一个就可以对字节或文本进行一些其他操作。
我可能是错的,但是我认为您的问题实际上是BytesIO当要传递给它的某些代码需要文本文件时,如何使用实例。
在这种情况下,这是一个常见问题,解决方案是编解码器模块。
以下是使用它的两种常见情况:
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'
Run Code Online (Sandbox Code Playgroud)
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'"
Run Code Online (Sandbox Code Playgroud)