Unable to read from io.StringIO
See original GitHub issue$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> buf = io.StringIO('hello world')
>>> buf.seek(0)
0
>>> import smart_open
>>> smart_open.open(buf).read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/codecs.py", line 500, in read
data = self.bytebuffer + newdata
TypeError: can't concat str to bytes
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
Fail to get data on using read() of StringIO in python
I need read() because my following code involves reading "n" bytes. python · stringio · Share.
Read more >Using StringIO to Read Delimited Text Files into NumPy
In this tutorial, we'll show you how to read delimited text data into a NumPy array using the StringIO package.
Read more >io — Core tools for working with streams — Python 3.11.1 ...
It deals with the reading and writing of bytes to a stream. FileIO subclasses RawIOBase to provide an interface to files in the...
Read more >StringIO Module in Python - GeeksforGeeks
The StringIO module is an in-memory file-like object. ... from io import StringIO ... Now we can able to read the file again()....
Read more >IO Tools (Text, CSV, HDF5, ...) - Pandas
For examples that use the StringIO class, make sure you import it according to your Python version, i.e. from StringIO import StringIO for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m not sure, would have to dig through the project history. I like @markopy 's argument, makes sense to keep smart_open’s mission clean and contained.
I don’t think StringIO has a
peek
method. Even if it did, other’s would rarely implement it. You would have to do aread
followed by rewinding withseek
which limits you to random access files.In general I’m not sure accepting file objects even makes sense. Why would you call smart_open or even the built in open if you already have a file object? I can see the point of having StringIO/BytesIO work for being able to mock something in memory but beyond that I’m not convinced.
Supporting StringIO also breaks the concept of smart_open always treating the underlying file as a byte stream which complicates things and may set you up for quite a bit of maintenance pain in the future.