mirror of
https://github.com/python/cpython.git
synced 2025-10-18 12:48:57 +00:00

svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r56808 | kurt.kaiser | 2007-08-07 14:15:45 -0700 (Tue, 07 Aug 2007) | 2 lines Replace sys.exitfunc call with atexit call ................ r56835 | thomas.wouters | 2007-08-08 09:20:30 -0700 (Wed, 08 Aug 2007) | 8 lines Tests for dict comprehensions I forgot to 'svn add' when committing the actual feature. I was _sure_ I had checked in these new tests already, but I guess the checkin failed and I didn't notice. ................ r56847 | guido.van.rossum | 2007-08-09 07:03:34 -0700 (Thu, 09 Aug 2007) | 30 lines Merged revisions 56760-56846 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r56762 | georg.brandl | 2007-08-06 00:37:58 -0700 (Mon, 06 Aug 2007) | 2 lines Place #ifdef Py_USING_UNICODE around decode_unicode(). ........ r56763 | georg.brandl | 2007-08-06 00:39:09 -0700 (Mon, 06 Aug 2007) | 2 lines Replace unnecessary function call. ........ r56764 | georg.brandl | 2007-08-06 00:52:20 -0700 (Mon, 06 Aug 2007) | 2 lines Patch #1765839: add link to https+proxy urllib2 opener. ........ r56797 | georg.brandl | 2007-08-07 00:13:24 -0700 (Tue, 07 Aug 2007) | 2 lines Bug #1769002: fix a now-wrong sentence in the tutorial. ........ r56830 | georg.brandl | 2007-08-08 06:03:41 -0700 (Wed, 08 Aug 2007) | 3 lines Revert the fix for #1548891, it broke backwards compatibility with arbitrary read buffers. Fixes #1730114. ........ r56833 | georg.brandl | 2007-08-08 06:50:02 -0700 (Wed, 08 Aug 2007) | 2 lines Fix compilation warning. ........ ................
125 lines
4 KiB
TeX
125 lines
4 KiB
TeX
\section{\module{StringIO} ---
|
|
Read and write strings as files}
|
|
|
|
\declaremodule{standard}{StringIO}
|
|
\modulesynopsis{Read and write strings as if they were files.}
|
|
|
|
|
|
This module implements a file-like class, \class{StringIO},
|
|
that reads and writes a string buffer (also known as \emph{memory
|
|
files}). See the description of file objects for operations (section
|
|
\ref{bltin-file-objects}).
|
|
|
|
\begin{classdesc}{StringIO}{\optional{buffer}}
|
|
When a \class{StringIO} object is created, it can be initialized
|
|
to an existing string by passing the string to the constructor.
|
|
If no string is given, the \class{StringIO} will start empty.
|
|
In both cases, the initial file position starts at zero.
|
|
|
|
The \class{StringIO} object can accept either Unicode or 8-bit
|
|
strings, but mixing the two may take some care. If both are used,
|
|
8-bit strings that cannot be interpreted as 7-bit \ASCII{} (that
|
|
use the 8th bit) will cause a \exception{UnicodeError} to be raised
|
|
when \method{getvalue()} is called.
|
|
\end{classdesc}
|
|
|
|
The following methods of \class{StringIO} objects require special
|
|
mention:
|
|
|
|
\begin{methoddesc}{getvalue}{}
|
|
Retrieve the entire contents of the ``file'' at any time before the
|
|
\class{StringIO} object's \method{close()} method is called. See the
|
|
note above for information about mixing Unicode and 8-bit strings;
|
|
such mixing can cause this method to raise \exception{UnicodeError}.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{close}{}
|
|
Free the memory buffer.
|
|
\end{methoddesc}
|
|
|
|
Example usage:
|
|
|
|
\begin{verbatim}
|
|
import StringIO
|
|
|
|
output = StringIO.StringIO()
|
|
output.write('First line.\n')
|
|
print >>output, 'Second line.'
|
|
|
|
# Retrieve file contents -- this will be
|
|
# 'First line.\nSecond line.\n'
|
|
contents = output.getvalue()
|
|
|
|
# Close object and discard memory buffer --
|
|
# .getvalue() will now raise an exception.
|
|
output.close()
|
|
\end{verbatim}
|
|
|
|
|
|
\section{\module{cStringIO} ---
|
|
Faster version of \module{StringIO}}
|
|
|
|
\declaremodule{builtin}{cStringIO}
|
|
\modulesynopsis{Faster version of \module{StringIO}, but not
|
|
subclassable.}
|
|
\moduleauthor{Jim Fulton}{jim@zope.com}
|
|
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
|
|
|
|
The module \module{cStringIO} provides an interface similar to that of
|
|
the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO}
|
|
objects can be made more efficient by using the function
|
|
\function{StringIO()} from this module instead.
|
|
|
|
Since this module provides a factory function which returns objects of
|
|
built-in types, there's no way to build your own version using
|
|
subclassing. Use the original \refmodule{StringIO} module in that case.
|
|
|
|
Unlike the memory files implemented by the \refmodule{StringIO}
|
|
module, those provided by this module are not able to accept Unicode
|
|
strings that cannot be encoded as plain \ASCII{} strings.
|
|
|
|
Calling \function{StringIO()} with a Unicode string parameter populates
|
|
the object with the buffer representation of the Unicode string, instead of
|
|
encoding the string.
|
|
|
|
Another difference from the \refmodule{StringIO} module is that calling
|
|
\function{StringIO()} with a string parameter creates a read-only object.
|
|
Unlike an object created without a string parameter, it does not have
|
|
write methods. These objects are not generally visible. They turn up in
|
|
tracebacks as \class{StringI} and \class{StringO}.
|
|
|
|
The following data objects are provided as well:
|
|
|
|
|
|
\begin{datadesc}{InputType}
|
|
The type object of the objects created by calling
|
|
\function{StringIO} with a string parameter.
|
|
\end{datadesc}
|
|
|
|
\begin{datadesc}{OutputType}
|
|
The type object of the objects returned by calling
|
|
\function{StringIO} with no parameters.
|
|
\end{datadesc}
|
|
|
|
|
|
There is a C API to the module as well; refer to the module source for
|
|
more information.
|
|
|
|
Example usage:
|
|
|
|
\begin{verbatim}
|
|
import cStringIO
|
|
|
|
output = cStringIO.StringIO()
|
|
output.write('First line.\n')
|
|
print >>output, 'Second line.'
|
|
|
|
# Retrieve file contents -- this will be
|
|
# 'First line.\nSecond line.\n'
|
|
contents = output.getvalue()
|
|
|
|
# Close object and discard memory buffer --
|
|
# .getvalue() will now raise an exception.
|
|
output.close()
|
|
\end{verbatim}
|
|
|