blob: 24312518f37279d38c235798798c1d61a69ef3d4 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{StringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +00002 Read and write strings as files}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{StringIO}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Read and write strings as if they were files.}
6
Guido van Rossumcda3d7d1997-03-03 16:01:21 +00007
Fred Drake8fe533e1998-03-27 05:27:08 +00008This module implements a file-like class, \class{StringIO},
Fred Drakeaf8a0151998-01-14 14:51:31 +00009that reads and writes a string buffer (also known as \emph{memory
Fred Drake83ff4af2000-11-28 16:24:28 +000010files}). See the description of file objects for operations (section
Fred Drake543e19d1999-04-21 18:15:22 +000011\ref{bltin-file-objects}).
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000012
Fred Drake8fe533e1998-03-27 05:27:08 +000013\begin{classdesc}{StringIO}{\optional{buffer}}
14When a \class{StringIO} object is created, it can be initialized
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000015to an existing string by passing the string to the constructor.
Fred Drake8fe533e1998-03-27 05:27:08 +000016If no string is given, the \class{StringIO} will start empty.
Raymond Hettingerfc113832005-04-11 01:03:44 +000017In both cases, the initial file position starts at zero.
Fred Drake83ff4af2000-11-28 16:24:28 +000018
19The \class{StringIO} object can accept either Unicode or 8-bit
20strings, but mixing the two may take some care. If both are used,
Fred Drake907e76b2001-07-06 20:30:11 +0000218-bit strings that cannot be interpreted as 7-bit \ASCII{} (that
Fred Drake83ff4af2000-11-28 16:24:28 +000022use the 8th bit) will cause a \exception{UnicodeError} to be raised
23when \method{getvalue()} is called.
Fred Drake8fe533e1998-03-27 05:27:08 +000024\end{classdesc}
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000025
Fred Drake7e9383a1998-04-11 18:05:24 +000026The following methods of \class{StringIO} objects require special
27mention:
28
Fred Drake8fe533e1998-03-27 05:27:08 +000029\begin{methoddesc}{getvalue}{}
30Retrieve the entire contents of the ``file'' at any time before the
Fred Drake83ff4af2000-11-28 16:24:28 +000031\class{StringIO} object's \method{close()} method is called. See the
32note above for information about mixing Unicode and 8-bit strings;
33such mixing can cause this method to raise \exception{UnicodeError}.
Fred Drake8fe533e1998-03-27 05:27:08 +000034\end{methoddesc}
35
36\begin{methoddesc}{close}{}
37Free the memory buffer.
38\end{methoddesc}
Fred Drake9463de21998-04-11 20:05:43 +000039
Andrew M. Kuchling9dd8dc32006-07-27 18:37:33 +000040Example usage:
41
42\begin{verbatim}
43import StringIO
44
45output = StringIO.StringIO()
46output.write('First line.\n')
47print >>output, 'Second line.'
48
49# Retrieve file contents -- this will be
50# 'First line.\nSecond line.\n'
51contents = output.getvalue()
52
53# Close object and discard memory buffer --
54# .getvalue() will now raise an exception.
55output.close()
56\end{verbatim}
57
Fred Drake9463de21998-04-11 20:05:43 +000058
Fred Drake295da241998-08-10 19:42:37 +000059\section{\module{cStringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +000060 Faster version of \module{StringIO}}
Fred Drakecd71aa21999-02-18 21:13:03 +000061
Fred Drakeb91e9341998-07-23 17:59:49 +000062\declaremodule{builtin}{cStringIO}
Fred Drake543e19d1999-04-21 18:15:22 +000063\modulesynopsis{Faster version of \module{StringIO}, but not
64 subclassable.}
Andrew M. Kuchlingc62af022004-01-08 15:01:08 +000065\moduleauthor{Jim Fulton}{jim@zope.com}
Fred Drakecd71aa21999-02-18 21:13:03 +000066\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drake9463de21998-04-11 20:05:43 +000067
68The module \module{cStringIO} provides an interface similar to that of
Fred Drake543e19d1999-04-21 18:15:22 +000069the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO}
Fred Drake9463de21998-04-11 20:05:43 +000070objects can be made more efficient by using the function
71\function{StringIO()} from this module instead.
72
73Since this module provides a factory function which returns objects of
74built-in types, there's no way to build your own version using
Fred Drake543e19d1999-04-21 18:15:22 +000075subclassing. Use the original \refmodule{StringIO} module in that case.
76
Fred Drake83ff4af2000-11-28 16:24:28 +000077Unlike the memory files implemented by the \refmodule{StringIO}
78module, those provided by this module are not able to accept Unicode
79strings that cannot be encoded as plain \ASCII{} strings.
80
Raymond Hettinger2adbb832003-01-31 05:17:33 +000081Another difference from the \refmodule{StringIO} module is that calling
82\function{StringIO()} with a string parameter creates a read-only object.
83Unlike an object created without a string parameter, it does not have
Skip Montanarobc961e52003-08-11 15:06:07 +000084write methods. These objects are not generally visible. They turn up in
85tracebacks as \class{StringI} and \class{StringO}.
Raymond Hettinger2adbb832003-01-31 05:17:33 +000086
Fred Drake543e19d1999-04-21 18:15:22 +000087The following data objects are provided as well:
88
89
90\begin{datadesc}{InputType}
91 The type object of the objects created by calling
92 \function{StringIO} with a string parameter.
93\end{datadesc}
94
95\begin{datadesc}{OutputType}
96 The type object of the objects returned by calling
97 \function{StringIO} with no parameters.
98\end{datadesc}
99
100
101There is a C API to the module as well; refer to the module source for
102more information.
Andrew M. Kuchling9dd8dc32006-07-27 18:37:33 +0000103
104Example usage:
105
106\begin{verbatim}
107import cStringIO
108
109output = cStringIO.StringIO()
110output.write('First line.\n')
111print >>output, 'Second line.'
112
113# Retrieve file contents -- this will be
114# 'First line.\nSecond line.\n'
115contents = output.getvalue()
116
117# Close object and discard memory buffer --
118# .getvalue() will now raise an exception.
119output.close()
120\end{verbatim}
121