question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
piskvorkycommented, Jan 21, 2021

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.

1reaction
markopycommented, Jan 20, 2021

I don’t think StringIO has a peek method. Even if it did, other’s would rarely implement it. You would have to do a read followed by rewinding with seek 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found