在python3中相对寻找io.StringIO

时间:2020-11-30 20:26:48

I am trying to refactor a python 2 package for use with python-3.x. The package uses StringIO.StringIO under python 2 and makes some use of the object's relative seek method, with statements like flob.seek(-1, 1). Unfortunately, the seek method of the corresponding io.StringIO object in python 3 does not support relative seeks, so the code raises

我正在尝试重构python 2包以与python-3.x一起使用。该软件包使用python 2下的StringIO.StringIO并使用对象的相对搜索方法,使用像flob.seek(-1,1)这样的语句。不幸的是,python 3中相应的io.StringIO对象的seek方法不支持相对搜索,因此代码会引发

OSError: Can't do nonzero cur-relative seeks

when trying to execute that statement.

在尝试执行该语句时。

What is the best way to refactor modules containing these calls, given that I would like to be able to continue using the functions this appears in for file objects as well as (objects derived from) strings?

重构包含这些调用的模块的最佳方法是什么,因为我希望能够继续使用此文件对象以及(从中派生的对象)出现的函数?

1 个解决方案

#1


8  

Because strings in Python 2 are renamed bytes in Python 3, the code should use io.BytesIO in Python 3, which supports relative seeking.

因为Python 2中的字符串在Python 3中被重命名为字节,所以代码应该使用Python 3中的io.BytesIO,它支持相对搜索。

#1


8  

Because strings in Python 2 are renamed bytes in Python 3, the code should use io.BytesIO in Python 3, which supports relative seeking.

因为Python 2中的字符串在Python 3中被重命名为字节,所以代码应该使用Python 3中的io.BytesIO,它支持相对搜索。