0

The documentation suggests that BytesIO is the new StringIO, because it supports current-relative seeks.

However, this is incorrect.

BytesIO cannot be used uniformly with TextIOWrappers as they are returned by open() calls. The former returns bytes the later returns text objects when reading.

TextIOWrapper(BytesIO(...)) also, does not do work as desired, because again, it does not support relative seeks.

So what is the best construct to replace the python2 StringIO in python3?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51

1 Answers1

1

There is no single uniform replacement, as string handling itself has changed in Python 3.

The class for in-memory text files in Python 3 is io.StringIO. Like other text files, it doesn't support current-relative seeks. While io.StringIO could theoretically support efficient current-relative seeks, for consistency with other text files (and to avoid constraining the implementation), it refuses to do so.

The class for in-memory binary files in Python 3 is io.BytesIO. There's a good chance that this is what you should be using (and if it is, then you should probably be opening your disk files in binary mode too).

If you really need the flexibility of Python 2's StringIO.StringIO.seek handling with an in-memory text file in Python 3, your best bet may be to write your own class.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • I guess, a ``Py2StringIO`` would be a good candidate for a class in ``io``. It could then be used automatically by the migration tool ``2to3``. String processing is the major pain during the migration, – Frank-Rene Schäfer Jan 14 '20 at 09:22
  • 1
    @Frank-ReneSchäfer: It'd never happen. Even if it wasn't far too late for something like that, such a class would be too much of an invitation for people to keep mixing up bytes and unicode. – user2357112 Jan 14 '20 at 09:27
  • Sad. That makes it difficult to do a smooth transition from 2 to 3. – Frank-Rene Schäfer Jan 14 '20 at 11:22
  • Update: The whole things with cur-relative seeking, even with ``TextIOWrappers(...)`` becomes messy. I decided to rewrite my code entirely to not use cur-relative seeks. Then, the good old new ``StringIO`` does the job. – Frank-Rene Schäfer Jan 20 '20 at 09:49