Besides the obvious (one is a type, the other a class)? What should be preferred? Any notable difference in use cases, perhaps?
除了明显的(一个是类型,另一个类)?什么应该是首选?用例中有任何明显的差异吗?
2 个解决方案
#1
26
http://docs.python.org/library/io.html#io.StringIO
http://docs.python.org/library/stringio.html
I see this.
我明白了
An in-memory stream for unicode text. It inherits TextIOWrapper.
用于unicode文本的内存中流。它继承了TextIOWrapper。
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).
该模块实现了一个类文件类StringIO,它读写字符串缓冲区(也称为内存文件)。
io.StringIO
is a class. It handles Unicode. It reflects the preferred Python 3 library structure.
io.StringIO是一个类。它处理Unicode。它反映了首选的Python 3库结构。
StringIO.StringIO
is a class. It handles strings. It reflects the legacy Python 2 library structure.
StringIO.StringIO是一个类。它处理字符串。它反映了传统的Python 2库结构。
What should be preferred?
什么应该是首选?
Always move forward toward the new library organization. The io.open
should be used to replace the built-in Unicode-unaware open
.
始终向新的图书馆组织迈进。应该使用io.open来替换内置的Unicode-unaware open。
Forward. Move forward.
前锋。前进。
#2
1
In terms of python 2.7 and 3:
就python 2.7和3而言:
io.BytesIO
is an in-memory file-like object that doesn't do any alteration to newlines, and is similar to open(filename, "wb")
. It deal with bytes()
strings, which in py2.7 is an alias for str
.
io.BytesIO是一个类似内存文件的对象,不对换行进行任何更改,类似于open(filename,“wb”)。它处理bytes()字符串,在py2.7中是str的别名。
io.StringIO
is an in-memory file-like object that does do alterations to newlines, and is similar to open(filename, "w")
. It deal with unicode()
strings, which in py3.x is an alias for str
.
io.StringIO是一个类似内存文件的对象,它确实对换行进行了更改,类似于open(filename,“w”)。它处理unicode()字符串,在py3.x中是str的别名。
py2.7's old StringIO.StringIO
is an in-memory file-like object that does not do alterations to newlines, and is similar to open(filename, "w")
. It deals with both unicode()
and bytes()
in the same way that most obsolete python 2 string methods do: by allowing you to mix them without error, but only as long as you're lucky.
py2.7的旧StringIO.StringIO是一个类似内存的文件对象,不会对换行进行更改,类似于open(filename,“w”)。它以与大多数过时的python 2字符串方法相同的方式处理unicode()和bytes():允许你混合它们而不会出错,但只要你很幸运。
Thus py2.7's old StringIO.StringIO
class is actually more similar to io.BytesIO than io.StringIO, as it is operating in terms of bytes()/str() and doesn't do newline conversions.
因此,py2.7的旧StringIO.StringIO类实际上比io.StringIO更类似于io.SytesIO,因为它以bytes()/ str()运行,并且不进行换行转换。
What should be preferred?
什么应该是首选?
Don't use StringIO.StringIO
, instead use io.BytesIO
or io.StringIO
, depending on the use-case. This is forward compatible with python 3 and commits to bytes or unicode, rather than "both, maybe".
不要使用StringIO.StringIO,而是使用io.BytesIO或io.StringIO,具体取决于用例。这与python 3向前兼容并提交到字节或unicode,而不是“两者,也许”。
#1
26
http://docs.python.org/library/io.html#io.StringIO
http://docs.python.org/library/stringio.html
I see this.
我明白了
An in-memory stream for unicode text. It inherits TextIOWrapper.
用于unicode文本的内存中流。它继承了TextIOWrapper。
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).
该模块实现了一个类文件类StringIO,它读写字符串缓冲区(也称为内存文件)。
io.StringIO
is a class. It handles Unicode. It reflects the preferred Python 3 library structure.
io.StringIO是一个类。它处理Unicode。它反映了首选的Python 3库结构。
StringIO.StringIO
is a class. It handles strings. It reflects the legacy Python 2 library structure.
StringIO.StringIO是一个类。它处理字符串。它反映了传统的Python 2库结构。
What should be preferred?
什么应该是首选?
Always move forward toward the new library organization. The io.open
should be used to replace the built-in Unicode-unaware open
.
始终向新的图书馆组织迈进。应该使用io.open来替换内置的Unicode-unaware open。
Forward. Move forward.
前锋。前进。
#2
1
In terms of python 2.7 and 3:
就python 2.7和3而言:
io.BytesIO
is an in-memory file-like object that doesn't do any alteration to newlines, and is similar to open(filename, "wb")
. It deal with bytes()
strings, which in py2.7 is an alias for str
.
io.BytesIO是一个类似内存文件的对象,不对换行进行任何更改,类似于open(filename,“wb”)。它处理bytes()字符串,在py2.7中是str的别名。
io.StringIO
is an in-memory file-like object that does do alterations to newlines, and is similar to open(filename, "w")
. It deal with unicode()
strings, which in py3.x is an alias for str
.
io.StringIO是一个类似内存文件的对象,它确实对换行进行了更改,类似于open(filename,“w”)。它处理unicode()字符串,在py3.x中是str的别名。
py2.7's old StringIO.StringIO
is an in-memory file-like object that does not do alterations to newlines, and is similar to open(filename, "w")
. It deals with both unicode()
and bytes()
in the same way that most obsolete python 2 string methods do: by allowing you to mix them without error, but only as long as you're lucky.
py2.7的旧StringIO.StringIO是一个类似内存的文件对象,不会对换行进行更改,类似于open(filename,“w”)。它以与大多数过时的python 2字符串方法相同的方式处理unicode()和bytes():允许你混合它们而不会出错,但只要你很幸运。
Thus py2.7's old StringIO.StringIO
class is actually more similar to io.BytesIO than io.StringIO, as it is operating in terms of bytes()/str() and doesn't do newline conversions.
因此,py2.7的旧StringIO.StringIO类实际上比io.StringIO更类似于io.SytesIO,因为它以bytes()/ str()运行,并且不进行换行转换。
What should be preferred?
什么应该是首选?
Don't use StringIO.StringIO
, instead use io.BytesIO
or io.StringIO
, depending on the use-case. This is forward compatible with python 3 and commits to bytes or unicode, rather than "both, maybe".
不要使用StringIO.StringIO,而是使用io.BytesIO或io.StringIO,具体取决于用例。这与python 3向前兼容并提交到字节或unicode,而不是“两者,也许”。