It's used when you have some API that only takes files, but you need to use a string. For example, to compress a string using the gzip module in Python 2:
import gzip
import StringIO
stringio = StringIO.StringIO()
gzip_file = gzip.GzipFile(fileobj=stringio, mode='w')
gzip_file.write('Hello World')
gzip_file.close()
stringio.getvalue()
Answer from Petr Viktorin on Stack OverflowIt's used when you have some API that only takes files, but you need to use a string. For example, to compress a string using the gzip module in Python 2:
import gzip
import StringIO
stringio = StringIO.StringIO()
gzip_file = gzip.GzipFile(fileobj=stringio, mode='w')
gzip_file.write('Hello World')
gzip_file.close()
stringio.getvalue()
StringIO gives you file-like access to strings, so you can use an existing module that deals with a file and change almost nothing and make it work with strings.
For example, say you have a logger that writes things to a file and you want to instead send the log output over the network. You can read the file and write its contents to the network, or you can write the log to a StringIO object and ship it off to its network destination without touching the filesystem. StringIO makes it easy to do it the first way then switch to the second way.
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.
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).
io.StringIO is a class. It handles Unicode. It reflects the preferred Python 3 library structure.
StringIO.StringIO is a class. It handles strings. It reflects the legacy Python 2 library structure.
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.
Forward. Move forward.
In terms of python 2.7 and 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.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.
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.
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.
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".