在python3中对io.StringIO的相对寻求 [英] relative seek for io.StringIO in python3
问题描述
我正在尝试重构与python-3.x一起使用的python 2软件包.该软件包在python 2下使用StringIO.StringIO
,并使用诸如flob.seek(-1, 1)
之类的语句来使用对象的相对查找方法.不幸的是,Python 3中相应的io.StringIO
对象的seek
方法不支持相对查找,因此代码引发了
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
OSError: Can't do nonzero cur-relative seeks
在尝试执行该语句时.
重构包含这些调用的模块的最佳方法是什么,考虑到我希望能够继续使用出现在文件对象以及(从对象派生的)字符串中的功能?
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?
推荐答案
因为 io.BytesIO
在Python 3中,它支持相对搜索.
Because strings in Python 2 are renamed bytes in Python 3, the code should use io.BytesIO
in Python 3, which supports relative seeking.
这篇关于在python3中对io.StringIO的相对寻求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!