在python3中对io.StringIO的相对寻求 [英] relative seek for io.StringIO in python3

查看:215
本文介绍了在python3中对io.StringIO的相对寻求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重构与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?

推荐答案

因为

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆