The io module provides the Python
interfaces to stream handling. Under
Python 2.x, this is proposed as an
alternative to the built-in file
object, but in Python 3.x it is the
default interface to access files and
streams.
Note Since this module has been
designed primarily for Python 3.x, you
have to be aware that all uses of
“bytes” in this document refer to the
str type (of which bytes is an alias),
and all uses of “text” refer to the
unicode type. Furthermore, those two
types are not interchangeable in the
io APIs.
在Python2.6/2.7中,io模块用于与Python3.X兼容
在3.X之前的Python版本中,StringIO模块包含StringIO的旧版本,与
io.StringIO
不同,StringIO可以在Python 2.6之前的版本中使用:你说:“这可能不太明显,但如果你使用StringIO处理二进制数据,你在Python 2.7或更新版本的上就走运了。”。
这并不明显,因为这不是真的。
如果您有在2.6或更早版本上工作的代码,它将继续在2.7上工作。未编辑的屏幕转储(Windows命令提示窗口在第80列及全部行换行):
如果需要编写在2.7和3.x上运行的代码,请使用
io
模块中的BytesIO
类。如果您需要/想要一个支持2.7、2.6、。。。3.x,你需要更加努力地工作。使用six模块应该会有很大帮助。
试试^{} 。
正如othershave所指出的,您确实可以在2.7中使用
StringIO
,但是BytesIO
是向前兼容的一个好选择。相关问题 更多 >
编程相关推荐